Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

No record count in PIVOT Expand / Collapse
Author
Message
Posted Wednesday, November 14, 2012 3:20 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 10, 2014 3:12 PM
Points: 37, Visits: 116
What's now the best way to add a grand total column at the end of the cross tab?
Post #1384898
Posted Wednesday, November 14, 2012 4:20 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Sunday, July 20, 2014 7:16 AM
Points: 945, Visits: 1,769
I would Suggest using a CTE to hold the pivot table then select your data and union it to a totals query.

This will get you close, i changed your dynamic sql a bit to use my tally table to give me some output i could use so you will have to modify this slightly.

declare @Columns varchar(max)
declare @Query nvarchar(max)
DECLARE @SumColumns NVARCHAR(MAX)

select @Columns = isnull(@Columns + ',[' + CAST(N AS VARCHAR) + ']', '[' + CAST(N AS VARCHAR) + ']')
from Tally
where N < 9

select @SumColumns = isnull(@SumColumns + ',SUM([' + CAST(N AS VARCHAR) + '])', 'SUM([' + CAST(N AS VARCHAR) + '])')
from Tally
where N < 9

SELECT @Query =

N'
declare @DaysMinus int
set @DaysMinus = 8;

WITH BasePvt AS (

select ''No of employees per '' + convert(varchar(10), PeriodEnd, 105) as KPI,
' + @Columns + '
from
(
select h.res_id as Resource,
pd.eddatum as PeriodEnd,
kp.oms25_0 as Costcenter

from humres h
left outer join perdat pd on convert(varchar(10), GETDATE()- @DaysMinus, 105) = convert(varchar(10), pd.eddatum, 105)
left outer join kstpl kp on h.costcenter = kp.kstplcode

where isnull(h.ldatuitdienst, getdate()+1) > pd.eddatum - @DaysMinus - and h.res_id > 5000
) as data

pivot (count(data.Resource) for data.Costcenter in ('+ @Columns +')) as pivottable
)

SELECT KPI,
' + @Columns + ' FROM BasePivot
UNION
SELECT ''Totals'', ' + @SumColumns + 'FROM BasePivot
'

SELECT @Query




For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden for the best way to ask your question.

For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw

Need to Split some strings? Jeff Moden's DelimitedSplit8K
Jeff Moden's Cross tab and Pivots Part 1
Jeff Moden's Cross tab and Pivots Part 2

Jeremy Oursler
Post #1384920
Posted Monday, November 19, 2012 3:08 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 10, 2014 3:12 PM
Points: 37, Visits: 116
Thanks a lot, but this adds a total row to the pivot.
What I would like to have is a total column at the end of the row that contains the total for that row.

I'm a bit confused about the results that I come across in Google. It didn't work out so far with my query.

Could you help me out with this one?

Thanks!
Post #1386258
Posted Monday, November 19, 2012 6:07 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Sunday, July 20, 2014 7:16 AM
Points: 945, Visits: 1,769
same concept as the total rows except instead of sum and you would use a second column variable to dynamically create column1 + column2 ... then instead of using a CTE you would instead use the working pivot table and just add that calculated column to the end of your select.


For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden for the best way to ask your question.

For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw

Need to Split some strings? Jeff Moden's DelimitedSplit8K
Jeff Moden's Cross tab and Pivots Part 1
Jeff Moden's Cross tab and Pivots Part 2

Jeremy Oursler
Post #1386337
Posted Thursday, November 22, 2012 4:06 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 10, 2014 3:12 PM
Points: 37, Visits: 116
Thanks for your reply!

I do something wrong but not sure what ...
I think the @SumColumns variable is not correct.
Hope you can help me out? Thanks

Here is my query:


DECLARE @Columns VARCHAR(MAX)
DECLARE @SumColumns VARCHAR(MAX)
DECLARE @Query nVARCHAR(MAX)

SELECT @Columns = ISNULL(@Columns + ',[' + kstplcode + ']', '[' + kstplcode + ']')
FROM kstpl
WHERE Enabled = 1

SELECT @SumColumns = ISNULL(@Columns + ',[' + @Columns + ']', '[' + @Columns + ']')
FROM kstpl
WHERE Enabled = 1


SET @Query =

N'
DECLARE @DaysMinus INT
SET @DaysMinus = 18;

--WITH BasePivot as

(
SELECT ''No of employees per '' + convert(VARCHAR(10), PeriodEnd, 105) as KPI,
' + @Columns + '
FROM
(
SELECT h.res_id as Resource,
pd.eddatum as PeriodEnd,
h.costcenter as Costcenter
--kp.oms25_0 as CComs
FROM humres h
LEFT OUTER JOIN perdat pd ON convert(VARCHAR(10), GETDATE()- @DaysMinus, 105) = convert(VARCHAR(10), pd.eddatum, 105)
LEFT OUTER JOIN kstpl kp ON h.costcenter = kp.kstplcode

WHERE ISNULL(h.ldatuitdienst, getdate()+1) > pd.eddatum - @DaysMinus /* Job runs on the last day of period */
AND h.res_id > 5000
GROUP BY pd.eddatum, h.costcenter, h.res_id
) as data

PIVOT (count(data.Resource) FOR data.Costcenter IN (' + @Columns + ')) as pivottable
)

SELECT
--KPI, ' + @Columns + ' FROM BasePivot
KPI, ' + @Columns + ', ' + @SumColumns + ' FROM pivottable
'

EXEC (@Query)
Post #1387822
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse