I have a TSQL and want to modify the select statements to create table and insert the selected commands.
set #query = N'SELECT ' + #cols + N' **create table tablename (insert into**
from
(
select substring, Field_Name,
rn = row_number() over(partition by field_name order by fieldnumber)
from bear_crossjoin
) x
pivot
(
max(substring)
for Field_Name in (' + #cols + N')
) p '
How can i define the one in Bold letters?
How about:
set #query = N'SELECT ' + #cols + N' INTO databasename.schemaname.tablename
from
(
select substring, Field_Name,
rn = row_number() over(partition by field_name order by fieldnumber)
from bear_crossjoin
) x
pivot
(
max(substring)
for Field_Name in (' + #cols + N')
) p '
Related
I have a simple table. As a result I need to get names of numeric columns and paste like rows and then paste its values like columns.Here is an example of table:
As a result I need to receive something like this:
I tried to get the result with PIVOT, but I have not the correct answer.
select * from (
select col1, 'val' + cast(row_number()over(partition by col1 order by col1) as nvarchar(20)) ColVal
from mytbl
) tmp
pivot (
min(col1) for ColVal in (val1,val2)
) pvt
In this case you need to unpivot first, then pivot back:
DROP TABLE IF EXISTS dbo.temp
DROP TABLE IF EXISTS dbo.temp2
CREATE table dbo.temp(col1 INT, col2 INT, col3 INT);
INSERT INTO temp VALUES (27,93,80),(32,84,72),(46,68,75),(38,79,73),(23,77,84);
DECLARE #colsUnpivot AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
-- first unpivot to key value pairs
select #colsUnpivot
= stuff((select ','+quotename(C.column_name)
from information_schema.columns as C
where C.table_name = 'temp' and
C.column_name like 'col%'
for xml path('')), 1, 1, '')
set #query
= 'SELECT ''val'' + convert(varchar, i) as id,
name,
val
INTO dbo.temp2
FROM
(
SELECT *, row_number() over (order by col1) as i
from temp
) a
UNPIVOT
(
val
FOR name IN ('+ #colsunpivot +')
) u'
exec sp_executesql #query;
-- now pivot back
DECLARE #columns NVARCHAR(MAX), #sql NVARCHAR(MAX);
SET #columns = N'';
SELECT #columns += N', ' + QUOTENAME(id)
FROM (SELECT DISTINCT id FROM dbo.temp2) AS x;
SET #query = N'
SELECT name, ' + STUFF(#columns, 1, 2, '') + '
FROM
(
SELECT id, name, val
from temp2
) AS j
PIVOT
(
SUM(val) FOR id IN ('
+ STUFF(REPLACE(#columns, ', [', ',['), 1, 1, '')
+ ')
) AS p;';
EXEC sp_executesql #query;
I adapted two separate scripts I had lying around, hence the two parts and the intermediate temp2 table. You can probably mash both together with a bit of elbow grease, but this should get you most of the way there.
Also adding the id (to get val1, val2 etc) dynamically means the results are sorted by col1 (val1 will have the lowest col1) but you were doing something similar in your attempt so I assume this is ok. If not, you will need to add an identity column to the data first and use that in place of the row_number()
Sorry if this isn't the best way to ask this but I am stuck at this point, I have tried researching but to no avail, trying to join these two queries:
SELECT [id]
,[title]
,[desc]
FROM [localTest].[dbo].[main];
DECLARE #cols AS NVARCHAR(MAX), #query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME([Year]) from [localTest].[dbo].[Years] FOR XML PATH(''),TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
set #query = 'SELECT [ID], ' + #cols + ' from (select [ID], [Year], [Amount] FROM [localTest].[dbo].[Years] ) x pivot (min([Amount]) for [Year] in (' + #cols + ')) p '
execute(#query);
Looking for the final result here:
enter image description here
I figured it out, here is my solution:
DECLARE #cols AS NVARCHAR(MAX);
SELECT #cols = STUFF((SELECT distinct ',' + QUOTENAME([Year]) FROM [localTest].[dbo].[Years] FOR XML PATH(''),TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'');
DECLARE #Sql VARCHAR(max);
SELECT #Sql = 'SELECT * FROM [localTest].[dbo].[main] a join (
SELECT * FROM (
SELECT [ID] id, [Year], [Amount]
FROM [localTest].[dbo].[Years]
) x pivot (
min([Amount])
for [Year] IN ('+#cols+')
) p ) as b on a.[ID] = b.id';
EXECUTE(#Sql);
Insert the result of second query to a #temp table and join the #temp table to main table like below:
SELECT [id]
,[title]
,[desc]
FROM [localTest].[dbo].[main];
DECLARE #cols AS NVARCHAR(MAX), #query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME([Year]) from [localTest].[dbo].[Years] FOR XML PATH(''),TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
set #query = 'SELECT [ID], ' + #cols + ' from (select [ID], [Year], [Amount] FROM [localTest].[dbo].[Years] ) x pivot (min([Amount]) for [Year] in (' + #cols + ')) p '
insert into #temp
execute(#query);
select * from [localTest].[dbo].[main] AS a inner join #temp as b on a.id = b.id
I have 2 tables :
and
.
Then I have this code which creates dynamic pivot table from 1st table and joins it with 2nd table.
DECLARE #cols AS NVARCHAR( MAX ),
#query AS NVARCHAR( MAX )
SET #cols = STUFF( ( SELECT ', ' + QUOTENAME( Nazev )
FROM Tab1
FOR XML PATH( '' ),
TYPE
).value( '.', 'NVARCHAR( MAX )' ),
1,
1,
''
)
SET #query = 'SELECT ZamestnanecID,' + #cols +
'FROM ( SELECT ZamestnanecID,
Tab1.Nazev AS Nazev2,
tab2.[castka]
FROM Tab1
JOIN Tab2 On Tab2.typ = Tab1.Id
) AS prePivot
PIVOT
(
SUM( castka )
FOR Nazev2 IN (' + #cols + ')
) p'
execute(#query)
And i need to somehow store the executed query so i can join it with tab3 by ZamestnanecId.It contains these columns: Jméno, Příjmení, ZaměstnanecId.
(i can't post image because of image limit)
Any ideas how to do it ?
It would be best to use executed query as "from" parameter in select but it is not necessary
You cannot store results but can instead continue using dynamic query like below
DECLARE
#cols AS NVARCHAR( MAX ),
#query AS NVARCHAR( MAX )
SET #cols = STUFF( ( SELECT ', ' + QUOTENAME( Nazev )
FROM Tab1
FOR XML PATH( '' ),
TYPE
).value( '.', 'NVARCHAR( MAX )' ),
1,
1,
''
)
SET #query = 'SELECT * FROM tab3 t3 JOIN '+
'( SELECT ZamestnanecID,' + #cols +
' FROM ( SELECT ZamestnanecID,
Tab1.Nazev AS Nazev2,
tab2.[castka]
FROM Tab1
JOIN Tab2 On Tab2.typ = Tab1.Id
) AS prePivot
PIVOT
(
SUM( castka )
FOR Nazev2 IN (' + #cols + ')
) p ) t '+
' on t.ZamestnanecId=t3.ZamestnanecId'
execute(#query)
I have dynamic sql query as below.
DECLARE #cols AS NVARCHAR(MAX) = '[0],[3],[11]',
#query AS NVARCHAR(MAX)
SET #query = N'SELECT * FROM
(
SELECT
year(createdDate) as [year],month(createdDate) as [month],cp.product_Id as product_ID,cp.salesprice as Amount
FROM customer_products cp
)s
PIVOT
(
SUM(Amount)
FOR product_Id IN ( '+ #cols +' ))
AS pvt;'
EXECUTE(#query)
Question:
Above query works however below query is not working because of
SELECT #cols = CONCAT(#cols, '[', cast(product_ID as varchar),']') FROM Product
code block.Error displays Incorrect syntax near
DECLARE #cols AS NVARCHAR(MAX) = '',
#query AS NVARCHAR(MAX)
SELECT #cols = CONCAT(#cols, '[', cast(product_ID as varchar),'],') FROM Product
SET #query = N'SELECT * FROM
(
SELECT
year(createdDate) as [year],month(createdDate) as [month],cp.product_Id as product_ID,cp.salesprice as Amount
FROM customer_products cp
)s
PIVOT
(
SUM(Amount)
FOR product_Id IN ( '+ #cols +' ))
AS pvt;'
EXECUTE(#query)
Where i miss exactly what is missing in above query while selecting productID from Product ?
You need to remove last , from #cols, add
SET #cols = LEFT(#cols, LEN(#cols) - 1)
You can consider using XML instead CONCAT like:
SELECT #cols = STUFF((SELECT ',' + QUOTENAME(CAST(product_ID as VARCHAR(10)))
FROM Products
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '');
It is a good practice to define length for cast(product_ID as varchar(10))
You need to ensure the last comma is removed from the select list, or you're passing an empty value to PIVOT.
Use this:
DECLARE #cols AS NVARCHAR(MAX) = '',
#query AS NVARCHAR(MAX)
SELECT #cols = COALESCE(#cols+', ','') + '[' + cast(product_ID as varchar) + ']' FROM product
SET #query = N'SELECT * FROM
(
SELECT
year(createdDate) as [year],month(createdDate) as [month],cp.product_Id as product_ID,cp.salesprice as Amount
FROM customer_products cp
)s
PIVOT
(
SUM(Amount)
FOR product_Id IN ( '+ #cols +' ))
AS pvt;'
EXECUTE(#query)
i need to make a 'where' statement in a dynamic column.
the dynamic column came from a row item.
sample as below.
SET #paramList = STUFF((
SELECT DISTINCT ',[' + parameter + ']'
FROM #tblitems FOR XML PATH('')
)
,1,1,'')
#paramList = [item1],[item2],[item3]
using the below query i need to incorporate the where statement at the end. but the column from the #paramlist should all be equal to 1 only.
SET #query ='select no,
' + #paramList + '
FROM( SELECT * FROM #tblitems)src
PIVOT
(
max(value)
for [parameter] in (' + #paramList + ')
) as piv order by item'
Create the condition string in the same way as the column list:
SET #condition = STUFF((
SELECT DISTINCT ' AND [' + parameter + '] = 1'
FROM #tblitems FOR XML PATH('')
)
,1,5,'');
Note though that you can also use the QUOTENAME function instead of enclosing the name in square brackets manually:
SET #condition = STUFF((
SELECT DISTINCT ' AND ' + QUOTENAME(parameter) + ' = 1'
FROM #tblitems FOR XML PATH('')
)
,1,5,'');
Now that you've got the condition string, you can add it to the dynamic query:
SET #query ='SELECT no,
' + #paramList + '
FROM (SELECT * FROM #tblitems) AS src
PIVOT
(
MAX(value)
FOR [parameter] IN (' + #paramList + ')
) AS piv
WHERE ' + #conditions + '
ORDER BY item;';