case语句不会转到else

时间:2014-09-26 15:42:39

标签: sql oracle

我想知道为什么当ENVIRON ='Dev / Int'没有行时,以下查询不会给出'N / A'。它在查询结果中返回null。我试过做NVL(COUNT(*)),但这也不起作用。

有什么想法吗?

提前致谢。

SELECT G1.NAME, 
  (SELECT CASE 
    WHEN COUNT(*) > 0 AND ticket IS NOT NULL THEN 'Solved'
    WHEN COUNT(*) > 0 AND ticket IS  NULL THEN 'Done'
    ELSE 'N/A'
  END
  FROM TABLE1
  WHERE ENVIRON='Dev/Int' AND G1.NAME=NAME GROUP BY ENVIRON, ticket ) "Dev/Int"
FROM TABLE1 G1 group by G1.NAME

1 个答案:

答案 0 :(得分:3)

它没有给出任何行,因为你要将它们全部过滤掉。 case 内部查询。如果没有要处理的行,则返回NULL。

我认为你只想要条件聚合。子查询似乎没必要:

SELECT G1.NAME, 
       (CASE WHEN SUM(CASE WHEN ENVIRON = 'Dev/Int' then 1 else 0 END) > 0 AND ticket IS NOT NULL
             THEN 'Solved'
             WHEN SUM(CASE WHEN ENVIRON = 'Dev/Int' then 1 else 0 END) > 0 AND ticket IS NULL
             THEN 'Done'
             ELSE 'N/A'
        END) as "Dev/Int"
FROM TABLE1
group by G1.NAME;

编辑:

糟糕,上面的ticket离开sum()。我认为你想要的逻辑票有sum()条件:

SELECT G1.NAME, 
       (CASE WHEN SUM(CASE WHEN ENVIRON = 'Dev/Int' AND ticket IS NOT NULL then 1 else 0 END) > 0
             THEN 'Solved'
             WHEN SUM(CASE WHEN ENVIRON = 'Dev/Int' AND ticket IS NULL then 1 else 0 END) > 0
             THEN 'Done'
             ELSE 'N/A'
        END) as "Dev/Int"
FROM TABLE1
group by G1.NAME;

我对原始查询的工作感到惊讶,并没有得到子查询返回多行的错误。