Solved

SQL Pivot table

Posted on 2014-01-23
10
337 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
Comment Utility
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
Comment Utility
I get "Invalid object name EE"
My table name is Pivot2
0
 
LVL 18

Expert Comment

by:x-men
Comment Utility
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
Comment Utility
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
Comment Utility
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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:murbro
Comment Utility
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
Comment Utility
you lost me.
what are you trying to accomplish?
0
 

Author Comment

by:murbro
Comment Utility
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
Comment Utility
Thanks for your help. I will post a new question to set out what I need
0
 

Author Comment

by:murbro
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

763 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

6 Experts available now in Live!

Get 1:1 Help Now