August 2, 2013 at 7:58 am
I am creating a table based on a dynamic PIVOT. This table is used for reporting, i'm emailing out the results using DB send mail. I need to add a total row to this table BEFORE I email. Here is the code that creates the table.
-- Construct the full T-SQL statement
-- and execute dynamically
SET @sql = N'SELECT Customer, ' + @selectcols +' into DailySalesTotals
FROM (SELECT Customer, sorderdate, isnull(qty,0) as Quantity
FROM dbo.dailysales) AS D
PIVOT(sum(quantity) FOR sorderdate IN(' + @cols + N')) AS P order by 2 desc;';
EXEC sp_executesql @sql;
I've thought about then doing a
Insert into DailySalesTotals
Select 'Total', 0,0,0,0,0,0,0
but, then how to update the zeros to the column totals without knowing the column headers? Is there a way to loop and do that? Or reference ordinal position? Would love some help and suggestions! Thanks!
August 2, 2013 at 8:25 am
insert the pivot into a temp table first, then select from that with you roll-up totals.
The probability of survival is inversely proportional to the angle of arrival.
August 2, 2013 at 8:37 am
sturner (8/2/2013)
insert the pivot into a temp table first, then select from that with you roll-up totals.
That still presents the same problem. Even if I put it in a temp table first, I have no idea what the column names are until the temp table is created. After I create that table how do I figure out what the column names are?
August 2, 2013 at 8:54 am
Get the column names from sys.columns -- if it is a table it will have a row in sys.objects and one row for each column in sys.columns. Here's a query that I use:
select Case when ROW_NUMBER() over (order by column_id) > 1 then ',' else '' end+C.name
from sys.sysobjects O
join sys.columns C on O.id = C.[object_id]
where O.type = 'U' and O.name = '##zzz'
order by column_id
The probability of survival is inversely proportional to the angle of arrival.
August 3, 2013 at 5:46 pm
tdanley (8/2/2013)
I am creating a table based on a dynamic PIVOT. This table is used for reporting, i'm emailing out the results using DB send mail. I need to add a total row to this table BEFORE I email. Here is the code that creates the table.-- Construct the full T-SQL statement
-- and execute dynamically
SET @sql = N'SELECT Customer, ' + @selectcols +' into DailySalesTotals
FROM (SELECT Customer, sorderdate, isnull(qty,0) as Quantity
FROM dbo.dailysales) AS D
PIVOT(sum(quantity) FOR sorderdate IN(' + @cols + N')) AS P order by 2 desc;';
EXEC sp_executesql @sql;
I've thought about then doing a
Insert into DailySalesTotals
Select 'Total', 0,0,0,0,0,0,0
but, then how to update the zeros to the column totals without knowing the column headers? Is there a way to loop and do that? Or reference ordinal position? Would love some help and suggestions! Thanks!
Depending on how you're doing the dynamic pivot, calculating the total and maybe even some interim subtotals could be a piece o' cake. But you need to post your dynamic pivot code in order for me to figure that out for you.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply