Solved

SQL Pivot table

Posted on 2014-01-23
10
346 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

752 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