分公司汇总,结果为0也要求显示的SQL怎样写?
t_a是存放公司信息的表,其中comcode是公司代码,唯一索引;t_b是存放销售单的表,其中有字段comcode,定单ID字段ddid,还有日期字段xdate;
现在要提取20070520当天各公司销售单的数量,即使数量为0也要求显示出来。应该怎么写?
select a.comcode,count(b.ddid)
from t_a a left join t_b on a.comcode=b.comcode
where b.xdate="20070520"
group by 1
可以汇总有销售单的公司,为0的就显示不出来了。
求助! 补充,如果用以下SQL
select a.comcode,count(b.ddid)
from t_a a left join t_b on a.comcode=b.comcode
where b.xdate="20070520" or b.xdate is null
group by 1
可以实现,但是速度太慢,而且总觉得不大理想。 select a.comcode,case when b.ddid is null then 0 else count(b.ddid) end
from t_a a left join t_b on a.comcode=b.comcode
where b.xdate="20070520"
group by 1 select a.comcode,count(b.ddid)
from t_a a left join t_b on a.comcode=b.comcode
where b.xdate="20070520"
group by 1
having count(b.ddid>=0)
不知道这样做可不可以实现 lavanyy的不行,之前就试过,只能显示不为0的。
yxdth2006的应该可行 楼主试试:
select a.comcode,nvl(count(b.ddid),0) count
from t_a a ,outert_bb
where a.comcode=b.comcode and b.xdate="20070520"
group by 1
页:
[1]