SQL一次性查询一个字段不同条件下的统计结果
参考了一下这篇文章:https://blog.csdn.net/xichenguan/article/details/51764100 , 感谢原作者
有两个表,分别存放了【操作员】和【单据】,要根据单据的不同类型来分类汇总(销售单、销售退货单,笔数和金额),并且显示在同一张表里,不想用做两次查询再合并的方法,研究了一下,终于搞定:
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<>''
order by ePersonCode asc
得到结果如下:

SQL一次性查询一个字段不同条件下的统计结果的更多相关文章
- sql关于对一个字段同时满足多条件判断来筛选查询
表所有数据 查询userName为abc或xyz的 以下为本菜鸟项目中遇到的问题: 背景: /** * wangjie 180629 * * 学生需要查询四种可能的消息 * 1.班级管理员发 ...
- 怎样用SQL语句查询一个数据库中的所有表?
怎样用SQL语句查询一个数据库中的所有表? --读取库中的所有表名 select name from sysobjects where xtype='u'--读取指定表的所有列名select nam ...
- mysql中查询一个字段属于哪一个数据库中的哪一个表的方式
mysql中查询一个字段具体是属于哪一个数据库的那一张表:用这条语句就能查询出来,其中 table_schema 是所在库, table_name 是所在表 --mysql中查询某一个字段名属于哪一个 ...
- 怎么用sql语句查询一个数据库有多少张表
今天在技术群中闲谈时忽然聊到一个问题,那就是当一个数据库中有多张表时怎么快速的获取到表的个数,从而给问询者一个准确的回答. 大家或许会说,这个问题和我们的数据库操作没有太大关系或者不是很挂钩,所以没意 ...
- SQL Server查询某个字段存在哪些表中
一.查询SQL Server中所有的表 SQL语句:SELECT * FROM sys.tables name列表示所有的表名. 二.查询SQL Server中所有的列 SQL语句:SELECT * ...
- sql查询一个字段不同值并返回
sql SELECT COUNT(字段),分组字段,SUM(字段),SUM(字段) FROM 表 GROUP BY 分组字段 java EntityWrapper<ProjectEntity&g ...
- mysql 中查询一个字段是否为null的sql
查询mysql数据库表中字段为null的记录: select * 表名 where 字段名 is null 查询mysql数据库表中字段不为null的记录: select * 表名 where 字段名 ...
- sql查询一个字段多列值合并为一列
SELECT GROUP_CONCAT(A.字段) AS 字段别名 FROM 表名 A WHERE A.字段=,,) SELECT GROUP_CONCAT(A.字段) AS 字段FROM 表名 A
- mysql 用sql语句查询一个表中的所有字段类型、注释
SELECT column_name,column_comment,data_type FROM information_schema.columns WHERE table_name='表名' AN ...
随机推荐
- MySQL设置远程连接服务器
默认情况下,mysql只允许本地登录,如果要开启远程连接,则需要修改/etc/mysql/my.conf文件. 一.修改/etc/mysql/my.conf找到bind-address = 127.0 ...
- 当spring抛出异常时出现的页面的@ExceptionHandler(RuntimeException.class) 用法
当spring抛出异常时出现的页面的@ExceptionHandler(RuntimeException.class) 用法 主要用在Controller层 @ExceptionHandler(Run ...
- 解决UnicodeDecodeError: 'ascii' code can't decode byte 0xef in position
今天在使用python的pip安装的时候出现了这个错误 UnicodeDecodeError: 'ascii' code can't decode byte 0xef in position 7: o ...
- SQL语句映射文件(1)resultMap
SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对应的接口类的路径.写好SQL语句映射文件后,需要在MyBAtis配置文件mappers标签中引用,例如: ...
- windows下python2.7版本numpy,Scipy,matplotlib,sklearn安装
系统是windows32位,安装了python2.7.13. 安装顺序就是numpy,Scipy,matplotlib,sklearn. 首先是更新一下pip (确保pip能使用) 然后将setupt ...
- POJ 3481 Double Queue(set实现)
Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...
- POJ 1577 Falling Leaves 二叉搜索树
HDU 3791 Falling Leaves 二叉搜索树 Figure 1Figure 1 shows a graphical representation of a binary tree of ...
- vue之生命周期的一点总结
vue的生命周期的过程提供了我们执行自定义逻辑的机会,好好理解它的生命周期,对我们很有帮助. 一.vue实例的生命周期(vue2.0) 二.生命周期描述:(参考截图) 三.例子 window.vm = ...
- 微信WeUI扩展组件
主要包括 下拉刷新pullToRefresh downRefresh.html 主要的代码是$(document.body).pullToRefresh(); <div class=" ...
- ecstore关于smarty语法调用
以下是smarty语法 转自http://www.phpwindow.com/ecstore_smarty2.html assign 属性 类型 是否必须 描述 var string yes 被赋值的 ...