本文作者:苏生米沿
本文地址:http://blog.csdn.net/sushengmiyan/article/details/50471210
需求很简单,我有一个部门和部门的请假申请表。表数据简单罗列如下:
申请信息存放在sqxx这张表中,然后存放了zybm和zybm对应的部门信息,有个部门表bmxx,想查询开始结束时间间隔为半天的(时差小于4即可)和大于半天的(按一天算)以部门为单位的统计结果,效果如下:
今天学了一个执行语句,顺利实现。感觉还是棒棒哒。
select sqbm, (select bmmc from bmxx b where b.bm=a.sqbm) as bmmc, sum(case when A.hasdata <>'' and timestampdiff(HOUR,kssj,jssj)<=4 then 1 else 0 end) as bt, sum(case when A.hasdata <>'' AND timestampdiff(HOUR,kssj,jssj) <=4 then 1 else 0 end) as qt from clgl_ycsqxx a group by a.sqbm;