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)
Related
I'm trying to replace NULL values with 0 in the following query, however without luck. I know I need to create another column list for the SELECT statement, but I've been unsuccessful so far.
How do I do this properly?
My query:
DECLARE #SQL AS NVARCHAR(MAX);
DECLARE #COLS AS NVARCHAR(MAX);
SELECT #COLS = ISNULL(#COLS + ',', '') + QUOTENAME(ItemCategoryName)
FROM
(
SELECT DISTINCT
ItemCategoryName
FROM sgdw.fact.RetailSales RS
JOIN SGDW.dim.Item I ON RS.ItemID = I.ItemID
WHERE RS.CalendarID >= '20190101'
AND storeid = '92'
AND DATALENGTH(ItemCategoryName) != 0
) AS Properties;
SET #SQL = N'SELECT *
FROM
(
SELECT RetailSalesAmountIncludingVATDKK,
ItemCategoryName,
ReceiptCode
FROM
(
SELECT *
FROM sgdw.fact.RetailSales
WHERE storeid = ''92''
UNION ALL
SELECT *
FROM sgdw.fact.RetailSales_hist
WHERE CalendarID >= ''20190101''
AND storeid = ''92''
) RS
JOIN SGDW.dim.Item I ON RS.ItemID = I.ItemID
JOIN SGDW.dim.Store S ON S.StoreID = RS.StoreID
) SourceTable
PIVOT (Sum(RetailSalesAmountIncludingVATDKK) FOR [ItemCategoryName] IN (' + #COLS + ')) as PivotTable';
EXEC sp_executesql
#SQL;
If you use SQL Server (starting with 2008), Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehousen you can bye COALESCE method
like
SELECT #COLS_SUM = #COLS_SUM + 'COALESCE(' + QUOTENAME(finmonth) + ',0)+'
FROM (SELECT DISTINCT finmonth FROM YOUR_TABLE ) AS tmp
SELECT #COLS_SUM = ','+ SUBSTRING(#COLS_SUM, 0, LEN(#COLS_SUM)) +' AS TOTAL'
for more info check https://www.codeproject.com/Questions/1391827/Dealing-with-nulls-dynamic-columns-in-pivot
Append This
SELECT #COLS = 'ISNULL('+REPLACE(#COLS,',',',0 ) , ISNULL(') + ',0 )'
After this block
SELECT #COLS = ISNULL(#COLS + ',', '') + QUOTENAME(ItemCategoryName)
FROM
(
SELECT DISTINCT
ItemCategoryName
FROM sgdw.fact.RetailSales RS
JOIN SGDW.dim.Item I ON RS.ItemID = I.ItemID
WHERE RS.CalendarID >= '20190101'
AND storeid = '92'
AND DATALENGTH(ItemCategoryName) != 0
) AS Properties;
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 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 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 '
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;';