SQLTeam.com | Weblogs | Forums

Row Data into Columns


#1

SELECT a.FirstName, a.LastName, a.Option_Typ, b.Phone
FROM tbl_ContactDetails

There are only two types of Option_Typ (SE, CE). The above query gives the below mentioned results.

FirstName , LastName , Option_Type , Phone
Zubin , K. , SE , 371-253
Zubin , K. , CE , 431-253
Map , I. , SE , 787-676
Map , I. , CE , 543-343

I want the Result to be

FirstName, LastName, SEOptionType, CEOptionType
Zubin , K. , 371-253 , 431-253
Map , I. , 787-676 , 543-343

How I can write the query?


#2

Great article on this type of problem here:

transposing rows to columns


#3

SELECT a.FirstName, a.LastName,
MAX(case when a.Option_Typ='SE' then b.Phone end) as SEOptionType,
MAX(case when a.Option_Typ='CE' then b.Phone end) as CEOptionType
FROM tbl_ContactDetails
GROUP BY
a.FirstName, a.LastName