Avatar of granate
granate
 asked on

SQL 2000 Query; How to Pivot Data?

Hi;

I have an SQL Server 2000 database table that I would like to query, but re-arrange the queried data into a recordset of a differnent layout.  

Current Database Layout with Data
Date            Yr_Num            Price
----------------------------------------------------------
01/01/2004      1            94.18
01/01/2004      2            92.11
01/01/2004      3            91.34
01/01/2004      4            90.09
01/01/2004      5            99.34
01/02/2004      1            97.49
01/02/2004      2            94.34
01/02/2004      3            95.78
01/02/2004      4            94.34
01/02/2004      5            94.66
01/03/2004      1            94.55
01/03/2004      2            94.24
01/03/2004      3            95.78
01/03/2004      4            94.34
01/03/2004      5            94.67


Desired Recordset via Query

Date             1        2          3        4          5
01/01/2004    94.18   92.11   91.34   90.09   99.34
01/02/2004    97.49   94.34   95.78   94.34   94.66
01/03/2004    94.55   94.24   95.78   94.34   94.67

Your help is greatly appreciated.  
Microsoft SQL Server

Avatar of undefined
Last Comment
granate

8/22/2022 - Mon
JimV_ATL

This should work, provided your dates are inserted without time.

declare @temp table ([date] datetime, yr_num int, price decimal(19,2))

insert into @temp values('01/01/2004',1,94.18)
insert into @temp values('01/01/2004',2,92.11)
insert into @temp values('01/01/2004',3,91.34)
insert into @temp values('01/01/2004',4,90.09)
insert into @temp values('01/01/2004',5,99.34)
insert into @temp values('01/02/2004',1,97.49)
insert into @temp values('01/02/2004',2,94.34)
insert into @temp values('01/02/2004',3,95.78)
insert into @temp values('01/02/2004',4,94.34)
insert into @temp values('01/02/2004',5,94.66)
insert into @temp values('01/03/2004',1,94.55)
insert into @temp values('01/03/2004',2,94.24)
insert into @temp values('01/03/2004',3,95.78)
insert into @temp values('01/03/2004',4,94.34)
insert into @temp values('01/03/2004',5,94.67)

select  [date],
sum( case when yr_num = 1 then price else 0 end) as '1',
sum( case when yr_num = 2 then price else 0 end) as '2',
sum( case when yr_num = 3 then price else 0 end) as '3',
sum( case when yr_num = 4 then price else 0 end) as '4',
sum( case when yr_num = 5 then price else 0 end) as '5'

from @temp
group by [date]
granate

ASKER
Thanks for the reply;

Like the answer, but it won't work if there are 10000 records in the table (several years worth).  Anyway to do what you recommended without coding out each date?  
JimV_ATL

The intial "insert into @temp" statements are just sample data.

You don't have to code out anything new for each date.  You do have to code a column for each possible value for yr_num.  How many possible values are there for yr_num?




This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
granate

ASKER
Hi;

A true example of the table would include 22 records for each date and there is approx. 10 years worth of data in the table ( well over 15000 records).  There will be 22 records, all with the same date value, but the yr num will be 1-22 and the Price will obviously vary.  

Thanks again;

Current Database Layout with Data
Date          Yr_Num          Price
----------------------------------------------------------
01/01/2004     1          94.18
01/01/2004     2          92.11
01/01/2004     3          91.34
01/01/2004     4          90.09
01/01/2004     5          99.34
01/01/2004     6          97.49
01/01/2004     7          94.34
01/01/2004     8          95.78
01/01/2004     9          94.34
01/01/2004     10        94.66
01/01/2004     11        94.55
01/01/2004     12        94.24
01/01/2004     13        95.78
01/01/2004     14        94.34
01/01/2004     15        94.67
01/01/2004     16        97.49
01/01/2004     17        94.34
01/01/2004     18        95.78
01/01/2004     19        94.34
01/01/2004     20        94.66
01/01/2004     21        94.55
01/01/2004     22        94.24
ASKER CERTIFIED SOLUTION
JimV_ATL

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
granate

ASKER
You're the man Jimmy