Solved

SQL Pivot Query

Posted on 2014-01-23
2
296 Views
Last Modified: 2014-01-23
Hi

I am trying to pivot data in the following format

Year           Month      Amount      Budget
2011      Jan              100              90
2011      Feb              40              30
2011      Mar              60              67
2011      Mar              15                5

to get a result like:
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

I used the following SQL Query when the raw data only had an Actual column:
Select * From [Pivot1] as S Pivot (Sum(Amount) For [Month] In (Jan,Feb, Mar, Apr)) As P
0
Comment
Question by:murbro
2 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39803832
try the below code

/*
Year           Month      Amount      Budget
2011      Jan              100              90
2011      Feb              40              30
2011      Mar              60              67
2011      Mar              15                5 
*/

/*
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 
*/

DECLARE @t TABLE 
(
 Year INT, MONTH CHAR(3), Amount INT, Budget INT
)
INSERT INTO @t VALUES
 (2011,'Jan',100,90)
,(2011,'Feb',40,30)
,(2011,'Mar',60,67)
,(2012,'Jan',2100,90)
,(2012,'Feb',240,30)
,(2012,'Mar',260,67)

SELECT Year,'Actual' AS Act_BUD,[jan],[Feb],[mar],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC] FROM
(SELECT Year,Month,Amount
FROM @t) P
PIVOT
(
    SUM(Amount) FOR Month IN([jan],[Feb],[mar],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC])
) PVT
UNION ALL
SELECT Year,'Budget' AS Act_BUD,[jan],[Feb],[mar],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC] FROM
(SELECT Year,Month,Budget
FROM @t) P
PIVOT
(
    SUM(Budget) FOR Month IN([jan],[Feb],[mar],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC])
) PVT

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 39803964
Thanks very much. That worked perfectly
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

912 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

16 Experts available now in Live!

Get 1:1 Help Now