?
Solved

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

Posted on 2010-01-12
7
Medium Priority
?
499 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
[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
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

800 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