来自多个查询的结果作为Oracle中的一个结果

时间:2019-07-16 17:48:57

标签: sql oracle

我具有多个条件的同一查询的结果如下所示:

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'

1 个答案:

答案 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;