With SQL, I have a table with key made of 1st three columns. All columns are small int values. There are a total of 21 columns per row.
I want to insert 80 new keys for every existing key. For example, first key is 1 201 0. I want to now update the table to also have keys 1 201 1, 1 201 2, 1 201 3, ... and keep the values present in the original row the same, duplicating the original row except for the new key structure.
I can do this with a program (read the table and then do the inserts in a loop), but I would like to know how to do this as a SQL statement.
If you are at least using SQL Server 2008 you can use the CROSS APPLY operator. This do the task:
DECLARE #T1 AS TABLE (ID INT)
DECLARE #T2 AS TABLE (ID1 INT, ID2 INT, ID3 INT)
INSERT INTO #T1 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10),
(11), (12), (13), (14), (15), (16), (17), (18), (19), (20),
(21), (22), (23), (24), (25), (26), (27), (28), (29), (30),
(31), (32), (33), (34), (35), (36), (37), (38), (39), (40),
(41), (42), (43), (44), (45), (46), (47), (48), (49), (50),
(51), (52), (53), (54), (55), (56), (57), (58), (59), (60),
(61), (62), (63), (64), (65), (66), (67), (68), (69), (70),
(71), (72), (73), (74), (75), (76), (77), (78), (79), (80)
INSERT INTO #T2 VALUES (1, 201, 0)
INSERT INTO #T2
SELECT ID1, ID2, ID3 + T1.ID
FROM #T2 AS T2
CROSS APPLY (SELECT ID
FROM #T1) AS T1
Just replace "#T2" with your table and, of course, comment the insert in "#T2"
Hope this helps.
Cheers
Also, if you are using SQL Server 2008, you can potentially use ROW_NUMBER().
Insert Into [Table]
(
[Key1]
,[Key2]
,[Key3]
,[DataColumn1]
,[DataColumn2]
)
(
SELECT
[Key1] + CAST( ROW_NUMBER() OVER (ORDER BY [Key1], [Key2], [Key3]) as int)
,[Key2] + CAST( ROW_NUMBER() OVER (ORDER BY [Key1], [Key2], [Key3]) as int)
,[Key3] + CAST( ROW_NUMBER() OVER (ORDER BY [Key1], [Key2], [Key3]) as int)
,[DataColumn1]
,[DataColumn2]
FROM [Table]
)
This will Insert all rows from Table ordering them by your 3 Key colums and add their row value to each of the individual Key columns. I don't know exactly what your data looks like, but this kind of query could create a collision on your 3 keys. You could possibly seed each ROW_NUMBER CAST with a value that is greater than the Max(Key). You just need to be creative with the Seed Value.
Insert Into [Table]
(
[Key1]
,[Key2]
,[Key3]
,[DataColumn1]
,[DataColumn2]
)
(
SELECT
[Key1] + CAST( ROW_NUMBER() OVER (ORDER BY [Key1], [Key2], [Key3]) as int) + SeedValue
,[Key2] + CAST( ROW_NUMBER() OVER (ORDER BY [Key1], [Key2], [Key3]) as int) + SeedValue
,[Key3] + CAST( ROW_NUMBER() OVER (ORDER BY [Key1], [Key2], [Key3]) as int) + SeedValue
,[DataColumn1]
,[DataColumn2]
FROM [Table]
)
If you want to be more selective regarding the rows to copy, you can use a where clause in the second select statement:
Insert Into [Table]
(
[Key1]
,[Key2]
,[Key3]
,[DataColumn1]
,[DataColumn2]
)
(
SELECT
[Key1] + CAST( ROW_NUMBER() OVER (ORDER BY [Key1], [Key2], [Key3]) as int) + SeedValue
,[Key2] + CAST( ROW_NUMBER() OVER (ORDER BY [Key1], [Key2], [Key3]) as int) + SeedValue
,[Key3] + CAST( ROW_NUMBER() OVER (ORDER BY [Key1], [Key2], [Key3]) as int) + SeedValue
,[DataColumn1]
,[DataColumn2]
FROM [Table] Where Key1=Something and Key2=Something
)
Related
I have an identity column in my table [ID]. I also have a computed column that is based on the identity column like so;
create table [dbo].[tblMaster] (
ID bigint identity(1,1),
GlobalFamilyUniqueID int,
DupeIdentifier as cast('EDME' + RIGHT('00000000'+ISNULL(cast([ID] as nvarchar(max)),''),8) as nvarchar(30)),
ControlNumber nvarchar(30),
NuixGuid nvarchar(50),
TopLvlGuid nvarchar(50),
ParentGuid nvarchar(50),
CustodianArtifactID int,
IsGlobalFamilyUnique int,
IsCustodianFamilyUnique int,
IsItemUnique int,
ItemUniqueDupID nvarchar(100),
IsChild int,
GroupIdentifier nvarchar(100),
DatasourceID int,
MD5Hash nvarchar(32),
GlobalFamilyDupID nvarchar(100),
CustodianFamilyDupId nvarchar(100),
ExportSessionID nvarchar(100)
)
I want to be update the ControlNumber column based on the IsGlobalFamilyUnique column. At the minute, DupeIdentifier will have a value regardless if IsGlobalFamilUnique is 1 or 0. I need ControlNumber to have an incremental value based on the last value in the ControlNumber field (I created another column called GlobalFamilyUnqiueID as a "counter" column) , but only when IsGlobalFamilUnique is 1.
This is where I am at so far;
update x
set
GlobalFamilyUniqueID = [ProposedGlobalFamilyUniqueID],
ControlNumber = [ProposedControlNumber]
from(
Select top 10 [id],
DupeIdentifier,
-- ControlNumber,
isglobalfamilyunique,
--GlobalFamilyUniqueID,
GlobalFamilyUniqueID,
Row_Number() Over(Order By [id]) [ProposedGlobalFamilyUniqueID],
ControlNumber,
'TEST' + RIGHT('00000000'+ISNULL(cast(Row_Number() Over(Order By [id]) as nvarchar(30)),''),8) [ProposedControlNumber]
From dbo.tblMaster mstr1
where IsGlobalFamilyUnique = 1 and ControlNumber is null
)x
The problem is that when the code is run again ProposedGlobalFamilyUniqueID will start at 1 again. It should start at the last value of ProposedGlobalFamilyUniqueID in dbo.tblMaster. I assume this is because I am limiting the query to where ControlNumber is null, but I don't know how to get around this.
This is an example of the code working successfully the first time its run:
The end result should be sequential ControlNumber values where IsGlobalFamilyUnique = 1
Example Data
declare #test table (
ID bigint identity(1,1),
GlobalFamilyUniqueID int,
DupeIdentifier as cast('EDME' + RIGHT('00000000'+ISNULL(cast([ID] as nvarchar(max)),''),8) as nvarchar(30)),
ControlNumber nvarchar(30),
MD5Hash nvarchar(32),
IsGlobalFamilyUnique bit
)
insert into #test (MD5Hash, IsGlobalFamilyUnique)values
--1
('ABC', 1),
--2
('DEF', 1),
--3
('GHI', 1),
--4
('JKL', 1),
--5
('ABC', 0),
--6
('XXX', 1)
The result should be;
Keep ControlNumber field as a number, try not to make it varchar because it will be not be able to calculate.
First of all remove all existing control number values before trying script.
Then,try this one:
DECLARE #max as bigint
SELECT #max = Max(id)
FROM tblMaster
Select [id],
DupeIdentifier,
-- ControlNumber,
isglobalfamilyunique,
--GlobalFamilyUniqueID,
GlobalFamilyUniqueID,
#max + Row_Number() Over(Order By [id]) [ProposedGlobalFamilyUniqueID],
ControlNumber
From dbo.tblMaster mstr1
where IsGlobalFamilyUnique = 1 and ControlNumber is null
update x
set
GlobalFamilyUniqueID = [ProposedGlobalFamilyUniqueID],
ControlNumber = x.Proposed_Control_Number
from(
Select [id],
DupeIdentifier,
-- ControlNumber,
isglobalfamilyunique,
--GlobalFamilyUniqueID,
GlobalFamilyUniqueID,
Row_Number() Over(Order By [id]) [ProposedGlobalFamilyUniqueID],
ControlNumber,
#max + Row_Number() Over(Order By [id]) as Proposed_Control_Number
From dbo.tblMaster mstr1
Where IsGlobalFamilyUnique = 1 and ControlNumber is null
)x
First post - I am trying to pull ten different pieces of information from a single field. Let me start with this is not my table, just what I was given to work with. This is a varchar max field.
'3350|#|1234567|~|3351|#|8/1/2017|~|3352|#|Acme|~|3353|~|10000.00|~|3354|#||~|3355|#||~3356|#|Yes|~|3357|#|Doe,John|~|3358|#|CA|~|3359|#|5551212'
I know that the numbers that start with 33 are keys telling me what information is in that section. 3350 has the invoice #1234567. 3351 has the invoice date of 8/1/17. etc. 3354 and 3355 were left null. The keys are unchanging and will be the same for every record in the table.
I need to pull the data from between 3350|#| and |~|3351 to get my invoice# and between 3351|#| and |~|3352 to get my date, etc, but I am struggling with how to word this. Any help would be appreciated and any critiques on my first post will be taken constructively.
The #YourTable is just a table variable used for demonstration / illustration
For Rows - Example
Declare #YourTable table (ID int,SomeCol varchar(max))
Insert Into #YourTable values
(1,'3350|#|1234567|~|3351|#|8/1/2017|~|3352|#|Acme|~|3353|#|10000.00|~|3354|#||~|3355|#||~|3356|#|Yes|~|3357|#|Doe,John|~|3358|#|CA|~|3359|#|5551212')
Select A.ID
,Item = left(RetVal,charindex('|#|',RetVal+'|#|')-1)
,Value = right(RetVal,len(RetVal)-charindex('|#|',RetVal+'|#|')-2)
From #YourTable A
Cross Apply [dbo].[udf-Str-Parse](A.SomeCol,'|~|') B
Returns
For Columns - Example
Declare #YourTable table (ID int,SomeCol varchar(max))
Insert Into #YourTable values
(1,'3350|#|1234567|~|3351|#|8/1/2017|~|3352|#|Acme|~|3353|#|10000.00|~|3354|#||~|3355|#||~|3356|#|Yes|~|3357|#|Doe,John|~|3358|#|CA|~|3359|#|5551212')
Select *
From (
Select A.ID
,Item = left(RetVal,charindex('|#|',RetVal+'|#|')-1)
,Value = right(RetVal,len(RetVal)-charindex('|#|',RetVal+'|#|')-2)
From #YourTable A
Cross Apply [dbo].[udf-Str-Parse](A.SomeCol,'|~|') B
) A
Pivot (max([Value]) For [Item] in ([3350],[3351],[3352],[3353],[3354],[3355],[3356],[3357],[3358],[3359]) ) p
Returns
The UDF if Interested
CREATE FUNCTION [dbo].[udf-Str-Parse] (#String varchar(max),#Delimiter varchar(10))
Returns Table
As
Return (
Select RetSeq = Row_Number() over (Order By (Select null))
,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>' + replace((Select replace(#String,#Delimiter,'§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A
Cross Apply x.nodes('x') AS B(i)
);
--Thanks Shnugo for making this XML safe
--Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
You can try a tally based splitter like below
declare #t table ( id int, col nvarchar(max));
insert into #t values
(1, '3350|#|1234567|~|3351|#|8/1/2017|~|3352|#|Acme|~|3353|~|10000.00|~|3354|#||~|3355|#||~3356|#|Yes|~|3357|#|Doe,John|~|3358|#|CA|~|3359|#|5551212')
,(2, '3350|#|123334567|~|3351|#|8/2/2017|~|3352|#|Acme|~|3353|~|10000.00|~|3354|#||~|3355|#||~3356|#|Yes|~|3357|#|Doe,John|~|3358|#|CA|~|3359|#|5551212');
select
id,
case
when split_values like '3350|#|%' then 'id'
when split_values like '3351|#|%' then 'date'
end as fieldname,
SUBSTRING(split_values,8,LEN(split_values)-7) as value
from
(
select
--t.col as col,
row_number() over (partition by t.col order by t1.N asc) as row_num,
t.id,
SUBSTRING( t.col, t1.N, ISNULL(NULLIF(CHARINDEX('|~|',t.col,t1.N),0)-t1.N,8000)) as split_values
from #t t
join
(
select
t.col,
1 as N
from #t t
UNION ALL
select
t.col,
t1.N + 3 as N
from #t t
join
(
select
top 8000
row_number() over(order by (select NULL)) as N
from
sys.objects s1
cross join
sys.objects s2
) t1
on SUBSTRING(t.col,t1.N,3) = '|~|'
) t1
on t1.col=t.col
)a
where
split_values like '3350|#|%' or
split_values like '3351|#|%'
Live demo
I have an Input table as under
Id Data
1 Column1: Value1
2 Column2: Value11
3 Column3: Value111
4 Column1: Value2
5 Column2: Value22
6 Column3: Value222
I am looking for an output as under
Column1 Column2 Column3
Value1 Value11 Value111
Value2 Value22 Value222
How can I achieve so? It could have been done easily by using a WHILE LOOP and by a bit of mathematical logic, but I am looking for a more optimized one if possible by only SELECT queries (no LOOPS).
I have tried also by splitting using (':') as delimiter and then transforming ROWS to COLUMNS (PIVOT) but somewhat could not be able to proceed. (That's my thought, peoples may have more better thoughts).
My shot so far
Declare #t table(Id int identity(1,1),Data varchar(1000))
Insert into #t Values
('Column1: Value1'),('Column2: Value11'),('Column3: Value111')
,('Column1: Value2'),('Column2: Value22'),('Column3: Value222')
Select *
FROM #t
SELECT
F1.id,
F1.Data,
O.splitdata
FROM
(
SELECT *,
cast('<X>'+replace(F.Data,':','</X><X>')+'</X>' as XML) as xmlfilter from #t F
)F1
CROSS APPLY
(
SELECT fdata.D.value('.','varchar(50)') as splitdata
FROM f1.xmlfilter.nodes('X') as fdata(D)) O
This will work if you want a pure SQL solution:
Select [Column1], [Column2], [Column3] From (
Select col, val, id = ROW_NUMBER() over(partition by d.col order by d.id)
From (
Select id
, col = LEFT(Data, CHARINDEX(':', Data)-1)
, val = RIGHT(Data, LEN(DATA) - CHARINDEX(':', Data))
From #t
) as d
) as p
pivot(
MAX(val)
FOR col in([Column1], [Column2], [Column3])
) as piv
But it supposes that data for Row 1 are always before data for Row 2. There is no way to distinguish them using your sample.
If the number of column is not fixed, it has to use Dynamic SQL.
SQL Server may not be the best options for this kind of thing.
With Dynamic SQL, the above query would be like this one:
create table #t(Id int identity(1,1),Data varchar(1000))
Insert into #t Values
('Column1: Value1'),('Column2: Value11'),('Column3: Value111')
,('Column1: Value2'),('Column2: Value22'),('Column3: Value222')
Declare #sql nvarchar(max)
Select #sql = '
Select '+left(c, len(c)-1)+' From (
Select col, val, id = ROW_NUMBER() over(partition by d.col order by d.id)
From (
Select id
, col = LEFT(Data, CHARINDEX('':'', Data)-1)
, val = RIGHT(Data, LEN(DATA) - CHARINDEX('':'', Data))
From #t
) as d
) as p
pivot(
MAX(val)
FOR col in('+left(c, len(c)-1)+')
) as piv
'
From (
Select Distinct '['+LEFT(Data, CHARINDEX(':', Data)-1)+'], '
From #t
FOR XML PATH('')
) as d(c)
EXEC sp_executesql #sql
SQL Fiddle
This should work:
Declare #t table(Id int identity(1,1),Data varchar(1000))
Insert into #t Values
('Column1: Value1'),('Column2: Value11'),('Column3: Value111')
,('Column1: Value2'),('Column2: Value22'),('Column3: Value222');
WITH Splitted AS
(
SELECT *
,CAST('<X>'+REPLACE(F.Data,':','</X><X>')+'</X>' AS XML) AS xmlfilter
FROM #t AS F
)
SELECT p.*
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY xmlfilter.value('X[1]','varchar(max)') ORDER BY Id) AS Inx
,xmlfilter.value('X[1]','varchar(max)') AS ColName
,xmlfilter.value('X[2]','varchar(max)') AS ColVal
FROM Splitted
) AS tbl
PIVOT
(
MAX(ColVal) FOR ColName IN(Column1,Column2,Column3)
) AS p
I do have a list of data having members Choice and IsRightAnswer for MCQ. I want to send this list to stored procedure that I did by having ',' as delimiter. Now in stored procedure I want to have choice and IsRightAnswer into two seperate table which I tried to do by separating them by odd and even index. I got stuck into ORDERBY condition of ROW_NUMBER. How can I do it efficiently?
DECLARE #temp table(Choice nvarchar(500), [rowCount] int IDENTITY(1,1))
DECLARE #tempIsRight table(IsRight bit, [rowCount] int IDENTITY(1,1))
;with tempChoice
as
(
select *,ROW_NUMBER() OVER (ORDER BY '') AS RowNumber
from dbo.Split(#Choice,',')
)
INSERT INTO #temp select * from tempChoice where RowNumber%2=0
;with tempIsRight
as
(
select *,ROW_NUMBER() OVER (ORDER BY '') AS RowNumber
from dbo.Split(#Choice,',')
)
INSERT INTO #tempIsRight select * from tempIsRight where RowNumber%2!=0
You can give dummy for ORDER BY like - (SELECT 1)
DECLARE #temp table(Choice nvarchar(500), [rowCount] int IDENTITY(1,1))
DECLARE #tempIsRight table(IsRight bit, [rowCount] int IDENTITY(1,1))
;with tempChoice
as
(
select *,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RowNumber
from dbo.Split(#Choice,',')
)
INSERT INTO #temp select * from tempChoice where RowNumber%2=0
;with tempIsRight
as
(
select *,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RowNumber
from dbo.Split(#Choice,',')
)
INSERT INTO #tempIsRight select * from tempIsRight where RowNumber%2!=0
Currently, I have 12 rows with column Named 'Value'. The sample like this (just sample data, real data will be more):
Value
1
2
3
4
6
7
8
9
10
11
12
14
What I want is select them to get result like this:
Result Result_Miss
1-4, 6-12, 14 5, 13
I want to avoid using a cursor to work row-by-row.
Dynamic, set-based approach using CTEs to hunt down the missing values, and write out the ranges available based on those missing values.
--(I can't seem to get SqlFiddle to work with CTE's or I'd post one up here)--
Reworked to be more dynamic for number of records:
This works provided you always have '1' in your set of value
CREATE TABLE #OneTen
(
Value INT NOT NULL
);
INSERT INTO #OneTen
VALUES (1), (2), (3), (4), (6), (8), (9), (10), (11), (12), (14);
WITH ExpectedActual AS
(
SELECT ot.Value AS Actual, ROW_NUMBER() OVER (ORDER BY Value) AS Expected
FROM #OneTen AS ot
)
, DegreesOff AS
(
SELECT ea.Expected, ea.Actual, (ea.Actual - ea.Expected) AS Change
FROM ExpectedActual AS ea
)
, Missing AS
(
SELECT CASE
WHEN MIN(do.Expected) = 1 THEN 0
ELSE MIN(do.Expected) + do.Change - 1
END AS Missing
, ROW_NUMBER() OVER (ORDER BY MIN(do.Expected)) AS RowNumber
FROM DegreesOff AS do
GROUP BY do.Change
UNION ALL
SELECT MAX(do.Actual + 1), MAX(do.Change + 2) --Adding Last Value 1 higher than Actual so the code below that takes mNext.Missing - 1 brings it down to the proper value:
--Change + 2 to account for 0 plus being 1 higher
FROM DegreesOff AS do
)
SELECT STUFF((
SELECT ', ' + CASE
WHEN m.Missing + 1 = mNext.Missing - 1 THEN CAST(m.Missing + 1 AS NVARCHAR(4))
ELSE CAST(m.Missing + 1 AS NVARCHAR(4)) + '-' + CAST(mNext.Missing - 1 AS NVARCHAR(4))
END
FROM Missing AS m
LEFT JOIN Missing AS mNext ON m.RowNumber = mNext.RowNumber - 1
FOR XML PATH('')), 1, 2, '') AS Result
, STUFF((
SELECT ', ' + CAST(MIN(do.Expected + do.Change - 1) AS NVARCHAR(4))
FROM DegreesOff AS do
WHERE do.Change > 0
GROUP BY do.Change
FOR XML PATH('')), 1, 2, '') AS Result_Miss
Try the following script:
DDL
CREATE TABLE Numbers
(
Value INT NOT NULL
);
INSERT INTO Numbers
VALUES (1), (2), (3), (4), (6), (7), (8), (9), (10), (12),(13);
Script
DECLARE #MinValue INT
DECLARE #MaxValue INT
DECLARE #Temp TABLE(MissingValues INT)
DECLARE #MissingValues VARCHAR(50)
SELECT #MinValue = MIN(Value),
#MaxValue = MAX(Value)
FROM Numbers
;WITH CTE AS
(
SELECT #MinValue Value
UNION ALL
SELECT Value + 1
FROM CTE
WHERE Value + 1 <= #MaxValue
)
INSERT INTO #Temp
SELECT CTE.Value
FROM CTE
LEFT JOIN Numbers N
ON CTE.Value = N.Value
WHERE N.Value IS NULL
OPTION (MAXRECURSION 1000)
SELECT #MissingValues =
STUFF(( SELECT ',' + CAST(MissingValues AS VARCHAR)
FROM #Temp
FOR XML PATH('')),1,1,'')
INSERT INTO #Temp
SELECT #MinValue - 1
UNION ALL
SELECT #MaxValue + 1
;WITH CTE AS
(
SELECT MissingValues,
ROW_NUMBER() OVER(ORDER BY MissingValues ASC) RN
FROM #Temp
)
,Ranges AS
(
SELECT CAST(T1.MissingValues + 1 AS VARCHAR) + '-' +
CAST(T2.MissingValues - 1 AS VARCHAR) Ranges
FROM CTE AS T1
INNER JOIN CTE AS T2
ON T1.RN = T2.RN - 1
)
SELECT STUFF(( SELECT ',' + R.Ranges
FROM Ranges R
FOR XML PATH('')),1,1,'') Result,
#MissingValues AS Result_Miss