I have a table that contains users, there phone numbers, and the type of phone
Table PersonPhone:
ID UserID PhoneType Phone
----- ------- ---- ------
1 101 1 #
2 101 2 #
3 102 1 #
4 102 3 #
5 103 1 #
6 103 2 #
7 103 3 #
8 104 1 #
9 104 3 #
10 104 3 #
I am trying to create a select statement that has a result set similar to what is below:
ID PhoneType1 PhoneType2 PhoneType3
------- ---- ------ ------
101 # # #
102 # # #
103 # # #
104 # # #
Is there anyway to do this? I've tried:
SELECT PersonID
, ISNULL(MAX(CASE phoneTypeId WHEN '2' THEN phone ELSE 0 END), 0) AS 'TYPE1'
, ISNULL(MAX(CASE phoneTypeId WHEN '3' THEN phone ELSE 0 END), 0) AS 'TYPE2'
, ISNULL(MAX(CASE phoneTypeId WHEN '5' THEN phone ELSE 0 END), 0) AS 'TYPE3'
FROM personPhone
GROUP BY personId