Solved

Totalling Pivot Table Output

Posted on 2012-03-14
2
234 Views
Last Modified: 2012-03-29
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 ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13])) AS p
0
Comment
Question by:SeTech
  • 2
2 Comments
 
LVL 51

Expert Comment

by:Mark Wills
Comment Utility
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...
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 500 total points
Comment Utility
Check to see if this works for you...

SELECT isnull(convert(char(5),[year]),'Total') as [Year]
       , sum([1]) as wk1
       , sum([2]) as wk2
       , sum([3]) as wk3
       , sum([4]) as wk4
       , sum([5]) as wk5
       , sum([6]) as wk6
       , sum([7]) as wk7
       , sum([8]) as wk8
       , sum([9]) as wk9
       , sum([10]) as wk10
       , sum([11]) as wk11
       , sum([12]) as wk12
       , sum([13]) as wk13
FROM   (SELECT year(ResolvedDate) AS [Year], Week, TicketID FROM vwWeek) AS s 
PIVOT  (count(TicketID) FOR Week IN ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13])) AS p
GROUP  BY [Year]
WITH ROLLUP

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now