group by如何保留count为0的结果?

来源:互联网 发布:电脑自带编程软件 编辑:程序博客网 时间:2024/04/28 10:05

我们知道,使用select count(*) from table1 where condition>100 group by ID时,结果中不会保留count为0的结果,如果我们需要保留count为0的结果怎么办?

 

不保留0的结果:

select GoodsTypeID,count(*) as Counts from tb_GoodsInfo
where GoodsPrice>1000
group by GoodsTypeID

 

保留0的结果:

select distinct a.GoodsTypeID,ISNULL(b.SubNum, 0) as Counts
from tb_GoodsInfo as a LEFT JOIN
(select GoodsTypeID,COUNT(1) as SubNum from tb_GoodsInfo as c
where c.GoodsPrice>1000
group by GoodsTypeID) as b
on a.GoodsTypeID = b.GoodsTypeID

原创粉丝点击