参考了一下这篇文章: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一次性查询一个字段不同条件下的统计结果的更多相关文章

  1. sql关于对一个字段同时满足多条件判断来筛选查询

    表所有数据 查询userName为abc或xyz的 以下为本菜鸟项目中遇到的问题: 背景: /**  * wangjie 180629  *   * 学生需要查询四种可能的消息  * 1.班级管理员发 ...

  2. 怎样用SQL语句查询一个数据库中的所有表?

    怎样用SQL语句查询一个数据库中的所有表?  --读取库中的所有表名 select name from sysobjects where xtype='u'--读取指定表的所有列名select nam ...

  3. mysql中查询一个字段属于哪一个数据库中的哪一个表的方式

    mysql中查询一个字段具体是属于哪一个数据库的那一张表:用这条语句就能查询出来,其中 table_schema 是所在库, table_name 是所在表 --mysql中查询某一个字段名属于哪一个 ...

  4. 怎么用sql语句查询一个数据库有多少张表

    今天在技术群中闲谈时忽然聊到一个问题,那就是当一个数据库中有多张表时怎么快速的获取到表的个数,从而给问询者一个准确的回答. 大家或许会说,这个问题和我们的数据库操作没有太大关系或者不是很挂钩,所以没意 ...

  5. SQL Server查询某个字段存在哪些表中

    一.查询SQL Server中所有的表 SQL语句:SELECT * FROM sys.tables name列表示所有的表名. 二.查询SQL Server中所有的列 SQL语句:SELECT * ...

  6. sql查询一个字段不同值并返回

    sql SELECT COUNT(字段),分组字段,SUM(字段),SUM(字段) FROM 表 GROUP BY 分组字段 java EntityWrapper<ProjectEntity&g ...

  7. mysql 中查询一个字段是否为null的sql

    查询mysql数据库表中字段为null的记录: select * 表名 where 字段名 is null 查询mysql数据库表中字段不为null的记录: select * 表名 where 字段名 ...

  8. sql查询一个字段多列值合并为一列

    SELECT GROUP_CONCAT(A.字段) AS 字段别名 FROM 表名 A WHERE A.字段=,,) SELECT GROUP_CONCAT(A.字段) AS 字段FROM 表名 A

  9. mysql 用sql语句查询一个表中的所有字段类型、注释

    SELECT column_name,column_comment,data_type FROM information_schema.columns WHERE table_name='表名' AN ...

随机推荐

  1. 更安全的https && https的问题

    视频推荐:https的性能优化 推荐文章:腾讯https性能优化实践 更安全的https(内容加密.身份认证.数据完整性) https实际上就是在http和tcp之间添加了ssl层或者是TLs层,这两 ...

  2. vue构造器以及实例属性

    一.构造器 1.vue.js就是一个构造器,通过构造器Vue来实例化一个对象:例如:var vm = new Vue({}); 2.实例化Vue时,需要传入一个参数(选项对象): 3.参数:选项对象可 ...

  3. Javac的命令

    关于命令,还可以查看<Java 7程序设计>一书后面的附录A As per javac source docs, there are 4 kinds of options: standar ...

  4. WPF DataTemplate與ControlTemplate

    一. 前言     什麼是DataTemplate? 什麼是ControlTemplate? 在stackoverflow有句簡短的解釋 "A DataTemplate, therefore ...

  5. Oracle 存储过程A

    create or replace procedure users_procedure is cursor users_cursor is select * from users; v_id user ...

  6. C++实现顺序查找,折半查找,插值查找

    1.顺序查找 从数组起始扫描到数组结尾,判断该索引数组是否和关键字相等,成功返回1 代码如下: //顺序查找 int seqSearch(int *array, int low, int high, ...

  7. C/C++编码规范

    C/C++编码规范 今天人们越来越明白软件设计更多地是一种工程,而不是一种个人艺术.由于大型产品的开发通常由很多的人协同作战,如果不统一编程规范,最终合到一起的程序,其可读性将较差,这不仅给代码的理解 ...

  8. VSCode基本配置

    功能:保存时自动按ESLint规范格式化代码 + stylus配置(需安装Manta's Stylus Supremacy插件) { "editor.fontSize": 18, ...

  9. SQL Server 导入excel时“该值违反了该列的完整性约束”错误

    SQL Server 导入excel时“该值违反了该列的完整性约束”错误 这个问题看似高大上,仔细分析了一下,ID列怎么会有重复呢? 原来是有很多空行呀!!! 所以导入excel时一定要注意空行的问题 ...

  10. LightningChart 客户案例分享-DCC 环境工程

    DCC Dynamics 致力于为建筑管控行业生产OEM的监控和管理产品.公司的旗舰产品“环境物流系统Environmental Logistics System” 用于大型建筑,校园及研究所设施,有 ...