SQL一次性查询一个字段不同条件下的统计结果
SQL一次性查询一个字段不同条件下的统计结果
参考了一下这篇文章:https://blog.youkuaiyun.com/xichenguan/article/details/51764100 , 感谢原作者

一次、两次、三次是同一个字段的不同值下的记录条数。
查询一个一列数据可以使用下面的SQL语句。
select COUNT(*) from shuili_company a
where a.check_number_ = 1 and a.is_delete = 0
from shuili_company GROUP BY province_
如果想用一条SQL语句将一次两次三次的同时查出来,可以使用下面的方式:
select province_,
isnull((select COUNT(*) from shuili_company a where a.check_number_ = 1 and a.is_delete = 0 AND a.province_ = shuili_company.province_),0) as row1,
isnull((select COUNT(*) from shuili_company a where a.check_number_ = 2 and a.is_delete = 0 AND a.province_ = shuili_company.province_),0) as row2,
isnull((select COUNT(*)from shuili_company a where a.check_number_ = 3 and a.is_delete = 0 AND a.province_ = shuili_company.province_),0) as row3,
isnull((select COUNT(*)from shuili_company a where a.sel_eval_number_ = 1 and a.is_delete = 0 AND a.province_ = shuili_company.province_),0) as row4
from shuili_company GROUP BY province_
另一大佬在此基础上做了两个表的查询https://www.cnblogs.com/skysowe/p/9117099.html
有两个表,分别存放了【操作员】和【单据】,要根据单据的不同类型来分类汇总(销售单、销售退货单,笔数和金额),并且显示在同一张表里,不想用做两次查询再合并的方法,研究了一下,终于搞定:
d_employee表

d_bilndx表

代码如下:
复制代码
select b.inputid as 开单员编号,
e.fullname as 开单员,
isnull( ( select count(*)
from d_bilndx
where draft=3 and biltype=12 and d_bilndx.inputid=e.id
), 0) as '销售开单笔数',
isnull( ( select sum(d_bilndx.amount)
from d_bilndx
where draft=3 and biltype=12 and d_bilndx.inputid=e.id
), 0) as '销售开单金额',
isnull( ( select count(*)
from d_bilndx
where draft=3 and biltype=13 and d_bilndx.inputid=e.id
), 0) as '销售退单笔数',
isnull( ( select sum(d_bilndx.amount)
from d_bilndx
where draft=3 and biltype=13 and d_bilndx.inputid=e.id
), 0) as '销售退单金额',
count(b.biltype) as 开单总笔数,
sum(b.Amount) as 开单金额
from d_bilndx as b
left join d_employee as e
on b.inputid=e.id
where b.draft=3 and ( b.biltype=12 or b.biltype=13 )
group by b.inputid, e.fullname, e.id
得到结果:

补记:以上代码有一个问题,就是如果没有符合条件的单据,查到的结果为空,而我们可能希望,查不到符合条件的记录,相关字段要显示为0(并且按天来统计),改写代码如下:
复制代码
select e1.id as ePersonCode, e1.FullName as eFullName,
isnull(Bill_Sale_NUm, 0) as Bill_Sale_Num,
isnull(Bill_Sale_Amount, 0) as Bill_Sale_Amount,
isnull(Bill_SaleReturn_Num, 0) as Bill_SaleReturn_Num,
isnull(Bill_SaleReturn_Amount, 0) as Bill_SaleReturn_Amount
from d_employee as e1
left join (
select b.inputid as ePersonCode,
e.fullname as eFullName,
isnull( ( select count(*)
from d_bilndx
where biltype=12 and d_bilndx.inputid=e.id and d_bilndx.date>='2018-06-03' and d_bilndx.date<='2018-06-03'
), 0) as Bill_Sale_Num,
isnull( ( select sum(d_bilndx.amount)
from d_bilndx
where biltype=12 and d_bilndx.inputid=e.id and d_bilndx.date>='2018-06-03' and d_bilndx.date<='2018-06-03'
), 0) as Bill_Sale_Amount,
isnull( ( select count(*)
from d_bilndx
where biltype=13 and d_bilndx.inputid=e.id and d_bilndx.date>='2018-06-03' and d_bilndx.date<='2018-06-03'
), 0) as Bill_SaleReturn_Num,
isnull( ( select sum(d_bilndx.amount)
from d_bilndx
where biltype=13 and d_bilndx.inputid=e.id and d_bilndx.date>='2018-06-03' and d_bilndx.date<='2018-06-03'
), 0) as Bill_SaleReturn_Amount,
count(b.biltype) as Bill_Total_Num
from d_employee as e
left join d_bilndx as b
on b.inputid=e.id
where (b.draft=3 or b.draft=2) and ( b.biltype=12 or b.biltype=13 ) and b.date>='2018-06-03' and b.date<='2018-06-03'
group by b.inputid, e.fullname, e.id
) as t1
on e1.id = t1.ePersonCode
where e1.id<>'00000'
order by ePersonCode asc
得到结果如下:

本文介绍如何使用SQL一次性查询同一字段在不同条件下的统计结果,包括一次、两次、三次的记录条数。通过示例展示了一条SQL同时查询多个条件的方法,并提供了一个解决没有匹配记录时显示0的解决方案。
4828

被折叠的 条评论
为什么被折叠?



