I have a table called phones which I need to return the top three phone numbers, and three types for each c_no in six separate columns and then join to the contacts table. The phone table is laid out like so,
p_no,c_no, phone, p_type
My desired result set would be,
c_no, First_Phone, First_Phone_Type, Second_Phone, Second_Phone_Type, Third_Phone, Third_Phone_Type
I tried the following, but it doesn't show anything in the second or third columns when I add the group by c_no
select c_no,
(case when cp_sort = 1 and cp_status = 1 then phone end) as Phone_1,
(case when cp_sort = 2 and cp_status = 1 then phone end) as Phone_2,
(case when cp_sort = 3 and cp_status = 1 then phone end) as Phone_3
from phones cp
group by
c_no
Thank you guys so much in advance for all the help!