1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| -- Début de vos données
with cte_tab1 (id, toto) as
(
select 1, 'toto' union all
select 2, 'toto' union all
select 3, 'toto'
)
, cte_tab2 (id, truc) as
(
select 3, 'truc' union all
select 4, 'truc' union all
select 5, 'truc'
)
, cte_tab3 (id, info) as
(
select 1, 'info' union all
select 6, 'info' union all
select 7, 'info'
)
, cte_tab4 (id, autre) as
(
select 2, 'autre' union all
select 6, 'autre' union all
select 8, 'autre'
)
-- Début de la requête
, cte_list_id (id) as
(
select id from cte_tab1 union
select id from cte_tab2 union
select id from cte_tab3 union
select id from cte_tab4
)
select ld.id
, coalesce(t1.toto, t2.truc, t3.info, t4.autre) as col1
, nullif(coalesce(t4.autre, t3.info , t2.truc, t1.toto ),
coalesce(t1.toto , t2.truc, t3.info, t4.autre)) as col2
from cte_list_id ld
left join cte_tab1 t1 on t1.id = ld.id
left join cte_tab2 t2 on t2.id = ld.id
left join cte_tab3 t3 on t3.id = ld.id
left join cte_tab4 t4 on t4.id = ld.id
order by ld.id asc;
ID COL1 COL2
---------- ----- -----
1 toto info
2 toto autre
3 toto truc
4 truc
5 truc
6 info autre
7 info
8 autre |
Partager