我具有多个条件的同一查询的结果如下所示:
SELECT COUNT(DISTINCT CASEID) FROM MYTABLE WHERE YR=2019 AND STATUS IN('W')
SELECT COUNT(DISTINCT CASEID) FROM MYTABLE WHERE YR=2019 AND STATUS IN('K') AND APSTAT='J'
SELECT COUNT(DISTINCT CASEID) FROM MYTABLE WHERE YR=2019 AND STATUS IN('L') AND RCODE='901'
SELECT COUNT(DISTINCT CASEID) FROM MYTABLE WHERE YR=2019 AND STATUS IN('L') AND
RCODE='910'
答案 0 :(得分:2)
您可以使用条件聚合:
SELECT
COUNT(DISTINCT CASE WHEN STATUS IN('W') THEN CASEID END)
,COUNT(DISTINCT CASE WHEN STATUS IN('K') AND APSTAT='J' THEN CASEID END)
,COUNT(DISTINCT CASE WHEN STATUS IN('L') AND RCODE='901' THEN CASEID END)
,COUNT(DISTINCT CASE WHEN STATUS IN('L') AND RCODE='910' THEN CASEID END)
FROM MYTABLE
WHERE YR=2019;