Solved

SQL Pivot Query

Posted on 2014-01-23
2
299 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 to return specific rows and columns, with various degrees of sorting and limits in place.

770 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