参考了一下这篇文章: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. Git的介绍和使用

    Git是目前世界上最先进的分布式版本控制系统 Git的安装 1.在linux上安装 你可以先输入git,看看系统是不是已经自带了git 或者 sudo apt-get install git  就可以 ...

  2. Javac之关于方法的选择

    15.12. Method Invocation Expressions 15.12.1. Compile-Time Step 1: Determine Class or Interface to S ...

  3. Linux 命令 su 和 sudo 比较

    su : 可以用来切换到任何用户,但是要求输入目标用户的密码 比如:su - su - root (等价 su -) su - user su root 与 su - root 的区别在于,后者不切换 ...

  4. goahead3.6.3就基本使用(后台上传信息到html页面),高手请忽略

    声明:这里面的代码均为网上找的然后有小小的改动,并非原创.但文章为原创 一..编译 1.1,.下载:进入http://embedthis.com/goahead/下载goahead3.6.3(2017 ...

  5. Ibatis框架之系统架构

    如果用最简洁的话来总结 iBATIS 主要完成那些功能时,我想下面几个代码足够概括. Class.forName("oracle.jdbc.driver.OracleDriver" ...

  6. Java 中 String 的常用方法(一)

    上一篇介绍了 String 中的几个常用构造方法,由 String 这个核心对象发散出去关于字符的编码,字符的字节表达,对 GC 的影响,正则表达式,模式匹配,这可能是 Java 里内涵最丰富的对象了 ...

  7. JavaScript自动化构建工具grunt、gulp、webpack介绍

    前端开发自动化工作流工具,JavaScript自动化构建工具grunt.gulp.webpack介绍 前端自动化,这样的一个名词听起来非常的有吸引力,向往力.当今时代,前端工程师需要维护的代码变得及为 ...

  8. kmp java implement--转

    http://cs.nyu.edu/~yap/classes/basic/progs/patternMatching/KMP.java /** * @file KMP.java * @synopsis ...

  9. 【WAN】PPPOE宽带上网功能详解

    1.mac地址克隆 某些地方的运营商会把宽带账号和用户电脑的mac地址绑定起来,运营商的账号只能让当前的电脑去上网,限制路由器上网,这时候路由器可以复制用户电脑的mac伪装成这台电脑去上网,实现路由器 ...

  10. CentOS 7重装mysql8.0.15

    查看是否安装有mysql rpm -qa | grep mysql 有的话先删除干净 下载MySQL yum源 wget https://dev.mysql.com/get/mysql80-commu ...