1. 程式人生 > >復雜的sql語句:按部門統計人數

復雜的sql語句:按部門統計人數

sco status and 計數 case when code end uid where

復雜的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語句:按部門統計人數