UNPIVOT function

set nocount on

–drop table #t

create table #t

(date datetime,
price1 int,
price2 int)
insert into #t VALUES (’01-dec-2012′,1,2)
insert into #t VALUES (’02-dec-2012′,3,4)
insert into #t VALUES (’03-dec-2012′,5,6)

–original table
select * from #t

–unpivoted table
select date,price_desc,price_val
from
(
select * from #t
) p
UNPIVOT
(
price_val for price_desc in (price1,price2)
) as u

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: