BernieSF
BernieSF

Reputation: 1828

SQL server pivot on table with join

I have this simple sql statement:

SELECT FCDT.description AS DetailType, FCD.data
FROM Contact_Detail FCD
LEFT JOIN Contact_Detail_Types FCDT
ON FCD.contact_detail_types_id=FCDT.contact_detail_types_id
WHERE contacts_id='4AA36958--0804CA'

which makes this result:

DetailType   data
-------------------------------
Phone        123-456-7890
Email        [email protected]

I need to convert this rows to columns, to have this result:

Phone         Email
--------------------------------
123-456-7890  [email protected]

How I can accomplish this? I have some hours playing with the pivot function, but no results so far.

Upvotes: 0

Views: 43

Answers (1)

Sravan Kumar
Sravan Kumar

Reputation: 51

Select * From
(SELECT FCDT.description AS DetailType, FCD.data As Data
FROM Contact_Detail FCD
LEFT JOIN Contact_Detail_Types FCDT
ON FCD.contact_detail_types_id=FCDT.contact_detail_types_id
WHERE contacts_id='4AA36958--0804CA')P
pivot
(Max(Data) for DetailType in ([Phone], [Email])) As Pvt

Upvotes: 1

Related Questions