Sunday, January 13, 2008

simple pivot table in TSQL (SQL Server 2000)

read original post: Pivot in mssql2000
select
t.myid as furnisher,
t1.myprice as price1_year,
t2.myprice price2_year from
(select distinct myid from mytable) t
join (select myid,myprice,rownum = (select count(*) from mytable t2 where t2.myid = t1.myid and t2.id <= t1.id) from mytable t1) t1 on
t.myid = t1.myid and t1.rownum = 1
join (select myid, myprice, rownum = (select count(*) from mytable t2 where t2.myid = t1.myid and t2.id <= t1.id) from mytable t1) t2 on
t.myid = t2.myid and t2.rownum = 2
order by 1

No comments: