SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


No record count in PIVOT


No record count in PIVOT

Author
Message
michielbijnen
michielbijnen
SSC Veteran
SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)

Group: General Forum Members
Points: 273 Visits: 145
What's now the best way to add a grand total column at the end of the cross tab?
CapnHector
CapnHector
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3321 Visits: 1789
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
michielbijnen
michielbijnen
SSC Veteran
SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)

Group: General Forum Members
Points: 273 Visits: 145
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!
CapnHector
CapnHector
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3321 Visits: 1789
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
michielbijnen
michielbijnen
SSC Veteran
SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)SSC Veteran (273 reputation)

Group: General Forum Members
Points: 273 Visits: 145
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search