Solved

Totalling Pivot Table Output

Posted on 2012-03-14
2
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 51

Expert Comment

by:Mark Wills
ID: 37723162
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
ID: 37723413
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

707 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