Solved

SQL Pivot Query

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

790 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