Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Totalling Pivot Table Output

Posted on 2012-03-14
2
Medium Priority
?
244 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 2000 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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

604 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