我有一个SQL查询这个样子,DB2 SQL左连接表的帮助
select
t1.id as ID,
case when t2.field1 = 1102 then (t2.field3 - t2.field2) end as A,
case when t2.field1 = 1112 then (t2.field3 - t2.field2) end as B,
case when t2.field1 = 1113 then (t2.field3 - t2.field2) end as C,
case when t2.field1 = 1106 then (t2.field3 - t2.field2) end as D
from table1 t1
left join table2 t2
on t1.id = t2.id
,结果是这样的;
ID A B C D
---- ------ ----- ----- ------
1773 100 NULL NULL NULL
1773 NULL 120 NULL NULL
1773 NULL NULL 200 NULL
1773 NULL NULL NULL 60
但我想显示这样的结果;
ID A B C D
---- ------ ----- ----- ------
1773 100 120 200 60
我该如何重写查询? thx为您的帮助..
是否所有的行都有相同的ID? –
是的,所有行都有相同的ID – vtokmak