SQLTeam.com | Weblogs | Forums

Unpivot column names


#1

Hi,
I have the following query
SELECT 'bobby' as Firstname , 10337 AS TotalForRetire, 5850 as TotalForRelatives,7337 as TotalForDisability,0 as dummy1, 0 as dummy2
The result is
Firstname TotalForRetire TotalForRelatives TotalForDisability
bobby 10337 5850 7337

I want to get the following:
Firstname LabelName LabelValue
Bobby TotalForRetire 10337
Bobby TotalForRelatives 5850
Bobby TotalForDisability 7337

Is this possible?
Thanks


#2
SELECT FirstName, LabelName, LabelValue FROM
(
	SELECT 'bobby' as Firstname , 10337 AS TotalForRetire, 5850 as TotalForRelatives,
	7337 as TotalForDisability,0 as dummy1, 0 as dummy2
)s
UNPIVOT (LabelValue FOR LabelName IN ([TotalForRetire],[TotalForRelatives],[TotalForDisability]))U

#3

Moved the topic to Transact-SQL.