Solved

SQL Pivot table

Posted on 2014-01-23
10
339 Views
Last Modified: 2014-01-23
Hi

I use the SQL further down to get the data from the first table below into the
format further on. I now have to add a fourth column called "Budget" and want
to include the sum of these amounts to Jan,Feb,Mar,Apr in the pivot as 4 new columns
How would I do this?

Year      Month      Amount
2011      Jan      100
2011      Feb      40
2011      Mar      60
2011      Mar      15
2011      May      50
2011      Jun      77
2012      Jul      70
2012      Aug      80

Select * From [Pivot1] as S Pivot (Sum(Amount) For [Month] In (Jan,Feb, Mar, Apr)) As P

Year       Jan      Feb      Mar      Apr
2011      100      40      75      NULL
2012      78      140      78      160
0
Comment
Question by:murbro
  • 6
  • 4
10 Comments
 
LVL 18

Expert Comment

by:x-men
ID: 39802776
Select * From (select *,
      sum([Amount]) over (partition by year) as budget
 from EE)
 as S Pivot (Sum(Amount) For [Month] In (Jan,Feb, Mar, Apr)) As P
0
 

Author Comment

by:murbro
ID: 39803067
I get "Invalid object name EE"
My table name is Pivot2
0
 
LVL 18

Expert Comment

by:x-men
ID: 39803072
Select * From (select *,
      sum([Amount]) over (partition by year) as budget
 from Pivot2)
 as S Pivot (Sum(Amount) For [Month] In (Jan,Feb, Mar, Apr)) As P
0
 

Author Comment

by:murbro
ID: 39803455
Thanks but still get errors
Msg 8156, Level 16, State 1, Line 4
The column 'budget' was specified multiple times for 'S'.
Msg 8156, Level 16, State 1, Line 4
The column 'budget' was specified multiple times for 'P'.
0
 
LVL 18

Accepted Solution

by:
x-men earned 500 total points
ID: 39803472
My test:


create table Pivot2 ([Year] int,[Month] char(3),[Amount] int)
insert into Pivot2 VALUES(2011,'Jan',100)
insert into Pivot2 VALUES(2011,'Feb',40)
insert into Pivot2 VALUES(2011,'Mar',60)
insert into Pivot2 VALUES(2011,'Mar',15)
insert into Pivot2 VALUES(2011,'May',50)
insert into Pivot2 VALUES(2011,'Jun',77)
insert into Pivot2 VALUES(2012,'Jul',70)
insert into Pivot2 VALUES(2012,'Aug',80)
insert into Pivot2 VALUES(2012,'Jan',78)
insert into Pivot2 VALUES(2012,'Feb',140)
insert into Pivot2 VALUES(2012,'Mar',78)


Select * From (select *,
      sum([Amount]) over (partition by year) as budget
 from Pivot2)
 as S Pivot (Sum(Amount) For [Month] In (Jan,Feb, Mar, Apr)) As P

Year|budget|Jan|Feb|Mar|Apr
2011|342|100|40|75|NULL
2012|446|78|140|78|NULL

(2 row(s) affected)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:murbro
ID: 39803512
Hi. Sorry to take so much of your time but my data has four columns
Year           Month      Amount      Budget
2011      Jan              100              90
2011      Feb         40              30
2011      Mar              60              67
2011      Mar              15                5
0
 
LVL 18

Expert Comment

by:x-men
ID: 39803544
you lost me.
what are you trying to accomplish?
0
 

Author Comment

by:murbro
ID: 39803581
A result like the following:

Year          Act_Bud      Jan      Feb      Mar      Apr
2011      Actual      100      40      20      100
2011      Budget      90      30      20      68
2012      Actual      94      34      14      94
2012      Budget      84      24      14      62
0
 

Author Closing Comment

by:murbro
ID: 39803770
Thanks for your help. I will post a new question to set out what I need
0
 

Author Comment

by:murbro
ID: 39803795
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now