While I was able to find how to pivot this data in these forums, I have not been able to find a means to push the results to a temp table so that I can use it for other queries. The code is the following. Is there a possible way to have the output of this populate a temp table?
SET #cols = STUFF((SELECT distinct ',' + QUOTENAME(QT.QUESTION_DESC)
FROM #QUES_TEMP QT
GROUP BY QT.QUESTION_DESC
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT EVAL_ID, AuditType, ' + #cols + '
into ##tmp
from
(
select QT.EVAL_ID,
QT.AuditType,
QT.SCORE,
QT.QUESTION_DESC
from #QUES_TEMP QT
) x
pivot
(
max(SCORE)
for QUESTION_DESC in (' + #cols + ')
) p '
execute(#query);
select * from ##tmp
You should be able to use INTO Clause. I added INTO into your example.
SET #cols = STUFF((SELECT distinct ',' + QUOTENAME(QT.QUESTION_DESC)
FROM #QUES_TEMP QT
GROUP BY QT.QUESTION_DESC
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT EVAL_ID, AuditType, ' + #cols + ' from
(
select QT.EVAL_ID,
QT.AuditType,
QT.SCORE,
QT.QUESTION_DESC
into ##tmp
from #QUES_TEMP QT
) x
pivot
(
max(SCORE)
for QUESTION_DESC in (' + #cols + ')
) p '
execute(#query);
SELECT * FROM ##tmp
SET #cols = STUFF((SELECT distinct ',' + QUOTENAME(QT.QUESTION_DESC)
FROM #QUES_TEMP QT
GROUP BY QT.QUESTION_DESC
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT EVAL_ID, AuditType, ' + #cols + '
from
(
select QT.EVAL_ID,
QT.AuditType,
QT.SCORE,
QT.QUESTION_DESC
from #QUES_TEMP QT
) x
pivot
(
max(SCORE)
for QUESTION_DESC in (' + #cols + ')
) p '
set #query = 'select * into ##Temp from ('+#query+') y'
execute(#query)
select * from ##Temp
Related
I am trying to cross join two pivot tables.
--First Pivot table query gets columns of drug_names and produces the row value
--of the drug_id.
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
Select #cols = STUFF((SELECT ',' + QUOTENAME(drug)
from _app_drugs
group by drug, drug_id,order_number
order by order_number
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = N'SELECT ' + #cols + N' from
(
select drug_id, drug
from _app_drugs
) x
pivot
(
max(drug_id)
for drug in (' + #cols + N')
) p'
exec sp_executesql #query;
--The second pivot table simply gets the signature_labels and displays the label name in the column and displays the label_id as the row value.
DECLARE #cols AS NVARCHAR(MAX),#scols as NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
Select #cols = STUFF((SELECT ',' + QUOTENAME(signature_label)
from _app_signature_labels
WHERE _app_signature_labels.isactive=1
group by signature_label_id, signature_label,ordernumber
order by ordernumber
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = N'SELECT ' + #cols + N' from
(
select signature_label_id,signature_label
from _app_signature_labels
) x
pivot
(
max(signature_label_id)
for signature_label in (' + #cols + N')
) p'
exec sp_executesql #query;
Now I just need to know how to combine these two pivot tables as one table... they don't have a common field and don't need one.
Can anybody help me with this one?
Thank you
You can execute two separate dynamic SQL statements into two global temp tables and then combine the result, like this:
IF object_id('tempdb..##tmpResult1') IS NOT NULL
BEGIN
DROP TABLE ##tmpResult1
END
IF object_id('tempdb..##tmpResult2') IS NOT NULL
BEGIN
DROP TABLE ##tmpResult2
END
DECLARE #cols AS NVARCHAR(MAX),
#cols2 AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX),
#query2 AS NVARCHAR(MAX)
Select #cols = STUFF((SELECT ',' + QUOTENAME(drug)
from _app_drugs
group by drug, drug_id,order_number
order by order_number
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = N'SELECT ' + #cols + N' INTO ##tmpResult1 from
(
select drug_id, drug
from _app_drugs
) x
pivot
(
max(drug_id)
for drug in (' + #cols + N')
) p'
exec sp_executesql #query;
--The second pivot table simply gets the signature_labels and displays the label name in the column and displays the label_id as the row value.
Select #cols2 = STUFF((SELECT ',' + QUOTENAME(signature_label)
from _app_signature_labels
WHERE _app_signature_labels.isactive=1
group by signature_label_id, signature_label,ordernumber
order by ordernumber
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query2 = N'SELECT ' + #cols2 + N' INTO ##tmpResult2 from
(
select signature_label_id,signature_label
from _app_signature_labels
) x
pivot
(
max(signature_label_id)
for signature_label in (' + #cols2 + N')
) p'
exec sp_executesql #query2;
SELECT * FROM ##tmpResult1 t1
INNER JOIN ##tmpResult2 t2 ON t1.ordernumber = t2.ordernumber
This is also a way to address this error when doing a large PIVOT query: Internal error: An expression services limit has been reached. Please look for potentially complex expressions in your query, and try to simplify them.
I have the following which I would like to be a View, but the Declare statement is not allowed apparently.
What is the best solution/work-around to this problem?
I'm using SQL Server. I have tried many other solutions (Stored Procedures, Table-Values Functions), but I think the combination of Dynamic SQL is complicating the matter and I haven't been able to get anything working.
DECLARE #cols AS NVARCHAR(MAX),
#colsAlias AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX);
select #cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(MeasurementTypeID)
FROM dbo.Measurements
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
SELECT #colsAlias = STUFF((SELECT DISTINCT ',' + QUOTENAME(dbo.Measurements.MeasurementTypeID) + ' AS ' + QUOTENAME(dbo.MeasurementTypes.MeasurementType)
FROM dbo.Measurements INNER JOIN dbo.MeasurementTypes ON dbo.Measurements.MeasurementTypeID = dbo.MeasurementTypes.MeasurementTypeID
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
SET #query = 'SELECT NeuronID, ' + #colsAlias +' FROM
(
SELECT MeasurementName, MeasurementValue, NeuronID, MeasurementTypeID
FROM dbo.Measurements
) x
PIVOT
(
MIN(MeasurementValue) FOR MeasurementTypeID IN (' + #cols + ')
) AS p'
EXECUTE(#query)
I have the following Code:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
SET #cols = STUFF((SELECT distinct ',' + QUOTENAME(month)
from PRCombinedRM
group by month,AccountNumber
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT AccountNumber,' + 'FullName,' + 'AccountType,' + 'Company,' + 'AccountBalance,' + #cols + ' from
(
select AccountNumber,
FullName,
AccountType,
Company,
AccountBalance,
month,
amount
from PRCombinedRM
) x
pivot
(
sum(amount)
for month in (' + #cols + ')
) p '
execute(#query)
However currently the results that this is outputting shows the values for "amount" as a NULL, however I would like to replace the NULL values with "0" instead.
How would I go about doing this?
Currently the data outputs as such:
AccountNumber FullName AccountType Company AccountBalance Aug Jul Jun Sep
100 M R Test Test Account Test Company 100 -50 -50 NULL -50
However I would like the data to output as:
AccountNumber FullName AccountType Company AccountBalance Aug Jul Jun Sep
100 M R Test Test Account Test Company 100 -50 -50 0 -50
Thank you.
I would use another variable to store the ISNULL(someColumn,0):
DECLARE #cols AS NVARCHAR(MAX), #query AS NVARCHAR(MAX);
DECLARE #cols2 AS NVARCHAR(MAX)
SET #cols = STUFF((SELECT distinct ',' + QUOTENAME(month)
from PRCombinedRM
group by month,AccountNumber
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'');
SET #cols2 = STUFF((SELECT distinct ', ISNULL(' + QUOTENAME(month) + ',0) ' + QUOTENAME(month)
from PRCombinedRM
group by month,AccountNumber
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'');
set #query = 'SELECT AccountNumber,' + 'FullName,' + 'AccountType,' + 'Company,' + 'AccountBalance,' + #cols2 + ' from
(
select AccountNumber,
FullName,
AccountType,
Company,
AccountBalance,
month,
Amount
from PRCombinedRM
) x
pivot
(
sum(amount)
for month in (' + #cols + ')
) p ';
execute(#query);
You can change the #cols definition:
SET #cols = STUFF((SELECT distinct ', coalesce(' + QUOTENAME(month) ', 0) as ' + QUOTENAME(month)
from PRCombinedRM
group by month,AccountNumber
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 2,'')
I have the following query to dynamically pivot some row information:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX);
select #cols = STUFF((SELECT ',' + QUOTENAME(convert(char(50), ScheduleEndTime, 120))
FROM metersNotRead
group by ScheduleEndTime
order by ScheduleEndTime DESC
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT ScheduleName, MeterID, CurrentGK,
' + #cols + ' from
(
select ScheduleName, MeterID, CurrentGK, ScheduleEndTime, ''Y'' flag
from metersNotRead
) x
pivot
(
max(flag)
for ScheduleEndTime in (' + #cols + ')
) p
order by ' + #cols +' DESC
'
execute(#query)
It gives me the correct results, but I am wondering how I can sort the rows by the first dynamic column, then the second, and so on, until all dynamic columns have been ordered by.
The results I have now are like this:
ScheduleName MeterID CurrentGK FirstDynamicCol SecondDynamicCol ETC
textName1 exampleID1 -- NULL Y
taxtName2 exampleID2 -- Y NULL
I want them to be like this:
ScheduleName MeterID CurrentGK FirstDynamicCol SecondDynamicCol ETC
textName2 exampleID2 -- Y NULL
taxtName1 exampleID1 -- NULL Y
Try this, put your first column name in its own variable, and order by that only.
DECLARE #cols AS NVARCHAR(MAX), #orderby NVARCHAR(MAX),
#query AS NVARCHAR(MAX);
select #orderby = STUFF((SELECT ',' + QUOTENAME(convert(char(50), ScheduleEndTime, 120)) + ' desc'
FROM metersNotRead
group by ScheduleEndTime
order by ScheduleEndTime DESC
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
select #cols = STUFF((SELECT ',' + QUOTENAME(convert(char(50), ScheduleEndTime, 120))
FROM metersNotRead
group by ScheduleEndTime
order by ScheduleEndTime DESC
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT ScheduleName, MeterID, CurrentGK,
' + #cols + ' from
(
select ScheduleName, MeterID, CurrentGK, ScheduleEndTime, ''Y'' flag
from metersNotRead
) x
pivot
(
max(flag)
for ScheduleEndTime in (' + #cols + ')
) p
order by ' + #orderby
execute(#query)
select #cols =
STUFF((SELECT ',' + QUOTENAME(grade)
from #temp
group by grade
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
select #colsRollup = STUFF((SELECT ', Sum(' + QUOTENAME(grade) + ') as '+QUOTENAME (grade)
from #temp
group by grade
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query
= 'SELECT * into #temp3 from
(
SELECT lob as "Vertical", '+ #colsRollup + '
FROM
(
SELECT lob,' + #cols + ' from
(
select lob,
grade,
asso_count
from #temp
) x
pivot
(
count(grade)
for grade in (' + #cols + ')
) p
) x1
GROUP BY lob with ROLLUP)x2'
execute(#query)
I am trying to create dynamic columns and insert it into a temporary table.
But it throws an error saying that #temp3 is not a valid object name. the code works if i dont try to insert the dynamic generated columns into a table.
If you execute the code and then try to access the #temp3table like:
execute(#query)
select * from #temp3
I would think it will have gone out of scope, which would explain the error message.
You could try to modify your query to include a SELECTlike:
SET #query = '
SELECT * INTO #temp3 FROM
(
SELECT lob as "Vertical", '+ #colsRollup + '
FROM
(
SELECT lob,' + #cols + ' from (select lob, grade, asso_count from #temp) x
PIVOT (count(grade) for grade in (' + #cols + ')) p
) x1
GROUP BY lob with ROLLUP
) x2; SELECT * FROM #temp3'
Note how the last line now includes ; SELECT * FROM #temp3
If you add that you should get the results back, if that is what you want.