We help IT Professionals succeed at work.

# Totalling Pivot Table Output

on
I am new with Pivot tables but the code below gives me what I want which is a ticket count by year. I am now asked to also report a bottom line total for the two years involved. Is there a way to make that part of this code?

Current Output
2011    data, data, data
2012    data, data, data

Desired Output
2011    data, data, data
2012    data, data, data
Total    data, data, data

SELECT     *
FROM         (SELECT     year(ResolvedDate) AS [Year], Week, TicketID
FROM          vwWeek) AS s PIVOT (count(TicketID) FOR Week IN (, , , , , , , , , , , , )) AS p
Comment
Watch Question

## View Solution Only

CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
Have you seen the "WITH ROLLUP" option on a query ? You do need to group by, and in your case that is just the year, so, seems a bit of overkill (considering it is the pivot key), but think it is the easiest.

Normally puts totals up the top (depends on group by), but there are ways to make it look right :)

Will be back in a few - will type up your pivot and have a play first...
CERTIFIED EXPERT
Distinguished Expert 2018
Commented:
Check to see if this works for you...

``````SELECT isnull(convert(char(5),[year]),'Total') as [Year]
, sum() as wk1
, sum() as wk2
, sum() as wk3
, sum() as wk4
, sum() as wk5
, sum() as wk6
, sum() as wk7
, sum() as wk8
, sum() as wk9
, sum() as wk10
, sum() as wk11
, sum() as wk12
, sum() as wk13
FROM   (SELECT year(ResolvedDate) AS [Year], Week, TicketID FROM vwWeek) AS s
PIVOT  (count(TicketID) FOR Week IN (, , , , , , , , , , , , )) AS p
GROUP  BY [Year]
WITH ROLLUP
``````