select (t1.a + t2.b) sum
from (select (aa + bb) a from table_x where cc = 'on') t1,
table_y t2
where t1.id = t2.id
问题是当找不到t1时,最终结果将为null;
如果找不到t1,如何将t2.b的默认值设为0?
提前谢谢。答案 0 :(得分:2)
如果您想要实际返回null
,如果找不到,则需要使用子查询或左连接。像这样:
select
nvl(
(select (aa + bb) from table_x where cc = 'on' and id = t2.id)
, 0) + t2.b as sum
from
table_y t2
答案 1 :(得分:0)
select (t1.a + decode(nvl(t1.a,-1),-1,0,t2.b) sum from (select (aa + bb) a from table_x where cc = 'on') t1, table_y t2 where t1.id = t2.id
这有用吗?如果t2.b是varchar选择,则可能需要用类似'X'的varchar替换-1,我猜不是;看着这里的补充。
答案 2 :(得分:0)
另一种选择是:
select ( nvl(sum(t1.a+t2.b),0))
from (select (aa + bb) a from table_x where cc='on') t1,
table_y t2
where t1.id = t2.id