Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 need unpivot sql

Author  Topic 

under2811
Constraint Violating Yak Guru

366 Posts

Posted - 2011-10-31 : 08:33:11
Hi All,

I need to do unpivoting operation for below results

Nov09--Dec09----Jan10
14-----29-------78

and want in format

Month----Count
Nov09----14
Dec09----29
Jan10----78

T.I.A

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-10-31 : 08:57:11
select [month] = 'Nov09', [Count] = nov09 from tbl
union all
select 'Dec09', Dec09 from tbl
union all
select 'Jan10', Jan10 from tbl


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-31 : 11:28:21
using unpivot


declare @tbl table
(
Nov09 int,
Dec09 int,
Jan10 int
)
insert @tbl
select 14,29,78

select [Month],val
from @tbl
unpivot(val for [Month] in (Nov09,Dec09,Jan10))u

output
-----------------------------------
Month val
-----------------------------------
Nov09 14
Dec09 29
Jan10 78



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -