如果行存在但我的“where”条件中没有金额,我需要将金额设为“0”。
原始命令是:
select t.aaa, count (t.bbb), sum (t.ccc)
from nrb t
where t.vvv IN ('3','4','5','6','D','E','F')
and t.ddd like '50%'
and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
group by t.aaa
order by t.aaa
结果是: excel文件中的“结果”选项卡。
我需要这个结果: excel文件中的“结果2”选项卡。
文件:我确实发送了完整的结果。 http://www.mediafire.com/?69cc4ay6cyt9cr9
我怎么能有这个?
Pl / sql 7.0.2无限用户许可
oci:9.2
oracle db:11.1.0.6.0 enterprise
os:win xp
答案 0 :(得分:1)
你可能想要:
select t1.aaa, coalesce(t2.bbb_count, 0) bbb_count,
coalesce(t2.ccc_sum, 0) ccc_sum
from (
select distinct aaa
from nrb
) t1
left join (
select t.aaa, count (t.bbb) bbb_count, sum (t.ccc) ccc_sum
from nrb t
where t.vvv IN ('3','4','5','6','D','E','F')
and t.ddd like '50%'
and t.eee >= TO_DATE('2012/03/21','YYYY/MM/DD')
and t.eee <= TO_DATE('2012/07/21','YYYY/MM/DD')
group by t.aaa
) t2 on t1.aaa = t2.aaa
order by t1.aaa;