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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kerrys57Author Commented:
This proved to be exactly what I was looking for and required the least amount of coding.  Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.