首页 > 数据库技术 > 详细

复杂的sql语句:按部门统计人数

时间:2019-03-16 20:40:14      阅读:622      评论:0      收藏:0      [点我收藏+]

复杂的sql语句,按部门统计人数:

--按部门统计人数
SELECT o.OUGUID AS OUGUID, o.OUNAME AS OUNAME,
IFNULL(COUNT(DISTINCT o.USERGUID), 0) AS USERNUM,
SUM(CASE WHEN IFNULL(q.CALLTIME, 0) != 0 THEN 1 ELSE 0 END) AS CallNoCount, 
SUM(CASE WHEN q.ROW_ID = 1 THEN 1 ELSE 0 END) AS QuestionCount,
SUM(CASE WHEN q.`STATUS` = 3 THEN 1 ELSE 0 END) AS PassCount,
SUM(CASE WHEN q.`STATUS` = 1 OR q.`STATUS` = 2 THEN 1 ELSE 0 END) AS BanjianCount
FROM 
    (SELECT ROW_ID,`STATUS`,handleuserguid,CALLTIME FROM audit_queue UNION ALL
        SELECT ROW_ID,`STATUS`,handleuserguid,CALLTIME FROM audit_queue_history) q 
LEFT JOIN (SELECT o.OUNAME,o.OUGUID,u.USERGUID, u.DISPLAYNAME FROM frame_user u 
                        LEFT JOIN frame_ou o ON u.OUGUID = o.OUGUID) o 
    ON q.handleuserguid = o.USERGUID 
WHERE q.CALLTIME BETWEEN 2018-09-30 00:00:00 AND 2018-10-30 23:59:59
GROUP BY o.OUGUID;
其中类似 SUM(CASE WHEN q.ROW_ID = ‘1‘ THEN 1 ELSE 0 END) AS QuestionCount 为按一定的条件计数;

复杂的sql语句:按部门统计人数

原文:https://www.cnblogs.com/wmqiang/p/10544042.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!