Wednesday, 15 April 2015

sql server - How to pivot rows to columns and column data under the new columns? -



sql server - How to pivot rows to columns and column data under the new columns? -

i have table http://sqlfiddle.com/#!3/641f9

and select this

how possible in sql server 2008 ? of import me, give thanks you.

i think want:

if object_id(n'tempdb..#temp') not null begin drop table #temp end select sd.company, sd.brand, 'datefrom' variable, sd.datefrom [date], sd.weekname #temp dbo.sourcedata sd union select sd.company, sd.brand, 'dateto' variable, sd.dateto [date], sd.weekname dbo.sourcedata sd select company, variable, brand, novw42012, mayw42014, augw32014, aprw12013, augw52013, marw22014 ( select company, variable, brand, [date], weekname #temp ) sd pivot ( min(sd.[date]) weekname in (novw42012, mayw42014, augw32014, aprw12013, augw52013, marw22014) ) pvt order company, brand

sql-server

No comments:

Post a Comment