Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to add totals to a row in a sql 2008 pivot stored procedure

Posted on 2010-01-12
7
Medium Priority
?
506 Views
Last Modified: 2012-05-08
Here is my existing code which works fine, but I need to add a total onto the end of each row. this must be fairly standard but I'm getting confused by the number of different approaches and I need something simple that works. Can anyone help?

ALTER PROCEDURE [dbo].[SPMT_JournalByMonthParam] (@Journal nvarchar(50))

select YEAR, [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'
      from
      (SELECT dbo.VW_MT_JournalCountByMonth.Year, dbo.VW_MT_JournalCountByMonth.Month, dbo.VW_MT_JournalCountByMonth.EnqCount
      from dbo.VW_MT_JournalCountByMonth
      where dbo.VW_MT_JournalCountByMonth.Journal= @Journal ) ps
      PIVOT
      (
      sum (enqcount)
      for Month in
      ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
      ) as pvt
This is a sample of my cuurent results
YEAR      Jan      Feb      Mar      Apr      May      June      July      Aug      Sept      Oct      Nov      Dec
2003      3      2      7      10      11      5      8      8      12      7      9      6
2004      4      4      2      3      3      2      1      NULL      NULL      2      2      2
2005      7      2      3      6      2      3      4      3      NULL      2      1      NULL
2006      3      2      1      NULL      3      NULL      NULL      NULL      NULL      NULL      1      NULL
2007      1      NULL      NULL      NULL      1      NULL      NULL      NULL      NULL      NULL      1      NULL
2008      NULL      NULL      1      NULL      NULL      NULL      NULL      NULL      NULL      NULL      NULL      NULL
2009      NULL      NULL      NULL      NULL      NULL      NULL      NULL      NULL      NULL      2      NULL      NULL

sorry it wraps around. I just need to show a total column for the year (row) at the end of the row.,

0
Comment
Question by:TrilobyteMKR
  • 5
7 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 26294566
Can you try this?
select YEAR, [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', total
      from
      (SELECT dbo.VW_MT_JournalCountByMonth.Year, 
		dbo.VW_MT_JournalCountByMonth.Month, 
		dbo.VW_MT_JournalCountByMonth.EnqCount
		sum(dbo.VW_MT_JournalCountByMonth.EnqCount) over (partition by dbo.VW_MT_JournalCountByMonth.Year) as total
	from dbo.VW_MT_JournalCountByMonth 
      where dbo.VW_MT_JournalCountByMonth.Journal= @Journal 
) ps
      PIVOT
      (
      sum (enqcount)
      for Month in
      ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
      ) as pvt

Open in new window

0
 
LVL 41

Expert Comment

by:ralmada
ID: 26294609
If the above brings duplicates you can try like this:

select YEAR, max([1]) as 'Jan', max([2]) as 'Feb', max([3]) AS 'Mar', max([4]) as 'Apr', max([5]) as 'May', max([6]) as 'June', max([7]) as 'July', max([8]) as 'Aug', max([9]) as 'Sept', max([10]) as 'Oct', max([11]) as 'Nov', max([12]) as 'Dec', max(total) as total
      from
      (SELECT dbo.VW_MT_JournalCountByMonth.Year, 
		dbo.VW_MT_JournalCountByMonth.Month, 
		dbo.VW_MT_JournalCountByMonth.EnqCount
		sum(dbo.VW_MT_JournalCountByMonth.EnqCount) over (partition by dbo.VW_MT_JournalCountByMonth.Year) as total
	from dbo.VW_MT_JournalCountByMonth 
      where dbo.VW_MT_JournalCountByMonth.Journal= @Journal 
) ps
      PIVOT
      (
      sum (enqcount)
      for Month in
      ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
      ) as pvt
group by YEAR

Open in new window

0
 
LVL 41

Expert Comment

by:ralmada
ID: 26294628
If not you can just do it like this:
select YEAR, [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) + and so on... as total
      from
      (SELECT dbo.VW_MT_JournalCountByMonth.Year, 
		dbo.VW_MT_JournalCountByMonth.Month, 
		dbo.VW_MT_JournalCountByMonth.EnqCount
	from dbo.VW_MT_JournalCountByMonth 
      where dbo.VW_MT_JournalCountByMonth.Journal= @Journal 
) ps
      PIVOT
      (
      sum (enqcount)
      for Month in
      ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
      ) as pvt

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 41

Expert Comment

by:ralmada
ID: 26294636
oops missed a comma there after column [12]
0
 
LVL 41

Accepted Solution

by:
ralmada earned 1000 total points
ID: 26294644
here's the attached code
select YEAR, [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) + and so on... as total 
      from 
      (SELECT dbo.VW_MT_JournalCountByMonth.Year,  
                dbo.VW_MT_JournalCountByMonth.Month,  
                dbo.VW_MT_JournalCountByMonth.EnqCount 
        from dbo.VW_MT_JournalCountByMonth  
      where dbo.VW_MT_JournalCountByMonth.Journal= @Journal  
) ps 
      PIVOT 
      ( 
      sum (enqcount) 
      for Month in 
      ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]) 
      ) as pvt

Open in new window

0
 
LVL 13

Expert Comment

by:sameer2010
ID: 26294692
I don't think row totals are possible in pivot. Column totals could be done using simple addition of columns in the select clause.
0
 

Author Closing Comment

by:TrilobyteMKR
ID: 31676158
Well done and a very speedy response.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

564 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