我希望有人可以提供帮助,关于我的案例:
如何从我的情况算起我的SQL代码中的条件如下:
SELECT CASE WHEN datediff( b.`DATE` , A.`SUBMIT_DATE` ) >3
THEN 'NOK'
ELSE 'OK'
END AS
STATUS FROM complaint a
LEFT JOIN handling b ON a.cid = b.cid
答案 0 :(得分:1)
SELECT
CASE
WHEN datediff( b.`DATE` , A.`SUBMIT_DATE` ) >3 THEN 'NOK'
ELSE 'OK'
END AS STATUS
FROM complaint a
LEFT JOIN handling b ON a.cid = b.cid
GROUP BY CASE
WHEN datediff( b.`DATE` , A.`SUBMIT_DATE` ) >3 THEN 'NOK'
ELSE 'OK'
END
或者,您可以将状态(OK,NOK)转换为不同的列:
SELECT
COUNT CASE WHEN datediff( b.`DATE` , A.`SUBMIT_DATE` ) >3 THEN 1 END) AS 'NOK',
COUNT CASE WHEN datediff( b.`DATE` , A.`SUBMIT_DATE` ) <=3 THEN 1 END) AS 'OK'
FROM complaint a LEFT JOIN handling b ON a.cid = b.cid
答案 1 :(得分:0)
它应该是这样的:D哈哈:
Select status,count(*)
From
(
SELECT CASE WHEN datediff( b.`DATE` , A.`SUBMIT_DATE` ) >3 THEN 'NOK' ELSE 'OK' END AS STATUS FROM complaint a LEFT JOIN handling b ON a.cid = b.cid
) a
Group by status
答案 2 :(得分:0)
尝试以下查询 -
SELECT CASE WHEN DATEDIFF( b.`DATE` , A.`SUBMIT_DATE` ) >3 THEN 'NOK' ELSE 'OK' END AS mystatus,
SUM(IF(DATEDIFF( b.`DATE` , A.`SUBMIT_DATE` ) >3,1,0)) AS cnt
FROM complaint a
LEFT JOIN handling b ON a.cid = b.cid
GROUP BY mystatus;