, :
create table a (id integer, flag boolean);
create table b (id integer, value_b varchar(30));
create table c (id integer, value_c varchar(30));
insert into a values (1, true), (2, false);
insert into b values (1, 'Val 1'), (2, 'Val 2');
insert into c values (1, 'C 1'), (2, 'C 2');
select a.id,
case when a.flag then b.value_b else c.value_c end AS value
from a
left join b using (id)
left join c using (id);
.
Of course, there are limitations:
- the number of columns is fixed, so you should go for
NULLif some values should be omitted; - you need to write
CASE ... ENDfor each column; - You must know all joined tables in advance.
- performance may not be the best.
source
share