I have following table:
ID | Type | Info
1 | Cell | 00123456789
1 | Mail | [email protected]
1 | Tel | 0123456
2 | Mail | [email protected]
.. | .. | ..
I want the following result:
ID | Cell | Mail | Tel
1 | 00123456789 | [email protected] | 0123456
2 | NULL | [email protected] | NULL
I tried this with cte's and joining them on ID, but this doesn't help for rows where (for example) the Cell number is empty. Any ideas?
As requested: the code I used (I edited this because it is a lot larger in my own database)
WITH cte_cell
AS
(
SELECT ID, TYPE, INFO AS cell
FROM table1
WHERE (TYPE = 'cell')
),
cte_mail
AS
(
SELECT ID, TYPE, INFO AS mail
FROM table1
WHERE TYPE = 'mail'
),
cte_tel
AS
(
SELECT ID, TYPE, INFO AS tel
FROM table1
WHERE TYPE = 'tel'
)
SELECT cte_cell.ID AS cte_cell_ID,
cte_mail.ID AS cte_mail_ID,
cte_tel.ID AS cte_tel_ID,
cte_cell.cell,
cte_mail.mail,
cte_tel.tel
FROM cte_cell FULL JOIN cte_mail
ON cte_cell.ID = cte_mail.ID
FULL JOIN cte_tel
ON cte_mail.ID = cte_tel.ID
;
