Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# SQL Pivot Query

Posted on 2014-01-23
Medium Priority
308 Views
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
Question by:Murray Brown
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 16

Accepted Solution

Surendra Nath earned 2000 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
0

Author Closing Comment

ID: 39803964
Thanks very much. That worked perfectly
0

## Featured Post

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
###### Suggested Courses
Course of the Month4 days, 14 hours left to enroll