Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

SQL Rollup with pivot sql 2008 R2

Avatar of TrilobyteMKR
TrilobyteMKRFlag for United Kingdom of Great Britain and Northern Ireland asked on
Microsoft SQL ServerMicrosoft SQL Server 2008
4 Comments1 Solution2406 ViewsLast Modified:
I have a pivot SP that works fine, but I want to add a total to the bottom of the result
Category, Jan, feb, mar ... Dec, total
Coffee 1,4,5,....2,30
TOTAL 2,7,11....5,57

I cannot get the toal at the bottom of the list as it returns a 'category ' with a null value, and so it appears at the top of the list.

Here is my SP

select PlacSourceType, [1] as 'Jan', [2] as 'Feb', [3] AS 'Mar', [4] as 'Apr', [5] as 'May', [6] as 'June', [7] as 'July', [8] as 'Aug', [9] as 'Sept', [10] as 'Oct', [11] as 'Nov', [12] as 'Dec',
      ISNULL([1], 0) + ISNULL([2], 0) + ISNULL([3], 0) + ISNULL([4], 0) + ISNULL([5], 0) + ISNULL([6], 0) + ISNULL([7], 0) + ISNULL([8], 0) + ISNULL([9], 0) + ISNULL([10], 0) + ISNULL([11], 0) + ISNULL([12], 0) as total

(SELECT     TOP (100) PERCENT YEAR(dbo.Tbl_LitBucket.EnqDate) AS Year, MONTH(dbo.tbl_litbucket.enqdate)as Month,
                      dbo.Tbl_Placing.PlacSourceType, SUM(dbo.Tbl_LitBucket.LitQty) AS LitQty
FROM         dbo.Tbl_LitBucket INNER JOIN
                      dbo.Tbl_Literature ON dbo.Tbl_LitBucket.LitID = dbo.Tbl_Literature.LitID INNER JOIN
                      dbo.Tbl_Placing ON dbo.Tbl_LitBucket.PlacID = dbo.Tbl_Placing.PlacID
WHERE     (dbo.Tbl_Literature.LitRetired IS NULL) and dbo.Tbl_Literature.TopLevelCategory=@category and dbo.Tbl_LitBucket.LitDescription=@description and  YEAR(dbo.Tbl_LitBucket.EnqDate)=@year
GROUP BY  YEAR(dbo.Tbl_LitBucket.EnqDate), Month(dbo.Tbl_LitBucket.EnqDate), dbo.Tbl_Placing.PlacSourceType with rollup
ORDER BY Year, Month, dbo.Tbl_Placing.PlacSourceType) ps
sum (LitQty)
for Month in
([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
      ) as pvt

Avatar of lundnak

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answers