Solved

SQL Pivot table

Posted on 2014-01-23
10
340 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
t-sql Joining Issue 10 41
SQL Insert parts by customer 12 34
Flattening heirachies 3 31
optimize stored procedure 6 29
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

821 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