Transpose Query Result

I have a query which returns a result such as the following:

(row) | Month | LowAmount | HighAmount (header)
1 | JAN | 10 | 20
2 | FEB | 15 | 45
3 | MAR | 13 | 26
4 | APR | 12 | 37

I want it to be able to transpose the columns and rows and end up with this:

(row) | AmountType | JAN | FEB | MAR | APR (header)
1 | LowAmount | 10 | 15 | 13 | 12
2 | HighAmount | 20 | 45 | 26 | 37

The left-hand column is the row number, the top row is the header.
In reality, I am working with a result that has 12 rows and 10 columns, but want to keep example simple.


kerrys57Asked:
Who is Participating?
 
ThomasianCommented:
You can use a combination of PIVOT and UNPIVOT.
DECLARE @t table ([row] int IDENTITY(1,1), [Month] char(3), LowAmount int, HighAmount int)
INSERT INTO @t ([Month], LowAmount, HighAmount)
SELECT 'JAN', 10, 20
UNION ALL SELECT 'FEB', 15, 45
UNION ALL SELECT 'MAR', 13, 26
UNION ALL SELECT 'APR', 12, 37

SELECT ROW_NUMBER() OVER (ORDER BY [AmountType]) [row], *
FROM
  (SELECT [Month], [Amount], [AmountType]
     FROM @t UNPIVOT ([Amount] FOR [AmountType] IN (LowAmount, HighAmount)) unpvt
  ) T PIVOT
  (MAX([Amount]) FOR [Month] IN ([JAN],[FEB],[MAR],[APR],[MAY],[JUN],[JUL],[AUG],[SEP],[OCT],[NOV],[DEC]))pvt

Open in new window

0
 
kenwagersCommented:
The simplest solution is to copy the result set into excel, and copy / paste special, selecting the Transpose option.

In SQL here's what I'd do, with the table name of 'claim', and the column to be checked for low and high values as 'claim_id'.  The Month (date field, in my case) is derived from the ts field (timestamp).

original Query:

select
datepart(month, ts) as MonthNo,
min(claim_id) as LowAmount,
max(claim_id) as HighAmount
from claim c
group by datepart(month,ts)
order by datepart(month,ts)

Transposed:

create table t_ResultSet
(AmountType char(10) not null,
 JAN int not null,
 FEB int not null,
 MAR int not null,
 APR int not null,
 MAY int not null,
 JUN int not null,
 JUL int not null,
 AUG int not null,
 SEP int not null,
 OCT int not null,
 NOV int not null,
 DEC int not null)
 
insert into t_ResultSet
(AmountType, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC)
SELECT
'LowAmount',
(select min(claim_id) from claim where datepart(month,ts) = 1) as JAN,
(select min(claim_id) from claim where datepart(month,ts) = 2) as FEB,
(select min(claim_id) from claim where datepart(month,ts) = 3) as MAR,
(select min(claim_id) from claim where datepart(month,ts) = 4) as APR,
(select min(claim_id) from claim where datepart(month,ts) = 5) as MAY,
(select min(claim_id) from claim where datepart(month,ts) = 6) as JUN,
(select min(claim_id) from claim where datepart(month,ts) = 7) as JUL,
(select min(claim_id) from claim where datepart(month,ts) = 8) as AUG,
(select min(claim_id) from claim where datepart(month,ts) = 9) as SEP,
(select min(claim_id) from claim where datepart(month,ts) = 10) as OCT,
(select min(claim_id) from claim where datepart(month,ts) = 11) as NOV,
(select min(claim_id) from claim where datepart(month,ts) = 12) as DEC
GO
insert into t_ResultSet
(AmountType, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC)
SELECT
'HighAmount',
(select max(claim_id) from claim where datepart(month,ts) = 1) as JAN,
(select max(claim_id) from claim where datepart(month,ts) = 2) as FEB,
(select max(claim_id) from claim where datepart(month,ts) = 3) as MAR,
(select max(claim_id) from claim where datepart(month,ts) = 4) as APR,
(select max(claim_id) from claim where datepart(month,ts) = 5) as MAY,
(select max(claim_id) from claim where datepart(month,ts) = 6) as JUN,
(select max(claim_id) from claim where datepart(month,ts) = 7) as JUL,
(select max(claim_id) from claim where datepart(month,ts) = 8) as AUG,
(select max(claim_id) from claim where datepart(month,ts) = 9) as SEP,
(select max(claim_id) from claim where datepart(month,ts) = 10) as OCT,
(select max(claim_id) from claim where datepart(month,ts) = 11) as NOV,
(select max(claim_id) from claim where datepart(month,ts) = 12) as DEC
GO

select * from t_resultSet
0
 
kerrys57Author Commented:
This proved to be exactly what I was looking for and required the least amount of coding.  Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.