带有子查询的SELECT CASE会覆盖多行

时间:2019-10-04 10:46:16

标签: oracle plsqldeveloper

当我执行partition_date = 2019-12-11和partition_date = 2019-07-06的代码时,我收到一条错误消息:“ 该子查询返回的行数超过一行”。在其他情况下有效

select case when my_count <=1 AND status_  = 'END' then 0 
                    when my_count >1 AND status_ IN ('END', 'RUNNING', 'START', 'ERR') THEN 1              
                    ELSE -1
               end  AS W 
        INTO return_val
        from (select 
          (SELECT count(DISTINCT(status)) FROM my_table 
          WHERE my_table.HIVE = 'hive1' 
          AND EXTRACT(MONTH FROM PARTITION_DATE) = EXTRACT(MONTH FROM TO_DATE('20191212', 'YYYY-MM-DD'))
          AND EXTRACT(YEAR FROM PARTITION_DATE) = EXTRACT(YEAR FROM TO_DATE('20191212', 'YYYY-MM-DD'))
        ) my_count,
          (SELECT DISTINCT(status) AS status_ FROM my_table 
          my_table.HIVE = 'hive1' 
          AND EXTRACT(MONTH FROM PARTITION_DATE) = EXTRACT(MONTH FROM TO_DATE('20191212', 'YYYY-MM-DD'))
          AND EXTRACT(YEAR FROM PARTITION_DATE) = EXTRACT(YEAR FROM TO_DATE('20191212', 'YYYY-MM-DD'))
        )status_
          FROM dual 
            )x  ;
           RETURN(return_val);

my_table就像

    STATUS |HIVE |PARTITION_DATE     |
    -------|-----|-------------------
    END    |hive1|2019-12-11 00:00:00|
    START  |hive1|2019-12-12 00:00:00|
    NA     |hive1|2019-12-13 00:00:00|
    END    |hive1|2019-09-12 00:00:00|
    END    |hive1|2019-09-13 00:00:00|
    END    |hive1|2019-09-14 00:00:00|
    ERR    |hive1|2019-08-06 00:00:00|
    ERR    |hive1|2019-07-06 00:00:00|
    START  |hive1|2019-07-07 00:00:00|
    RUNNING|hive1|2019-07-08 00:00:00|
    END    |hive1|2019-07-09 00:00:00|

2 个答案:

答案 0 :(得分:0)

您在声明中有一些地位:

(SELECT DISTINCT(status) AS status_ FROM my_table 
          WHERE EL_F_BDP_PROC_REALTIME.HIVE = 'hive1' 
          AND EXTRACT(MONTH FROM PARTITION_DATE) = EXTRACT(MONTH FROM TO_DATE('20191212', 'YYYY-MM-DD'))
          AND EXTRACT(YEAR FROM PARTITION_DATE) = EXTRACT(YEAR FROM TO_DATE('20191212', 'YYYY-MM-DD'))
        )

并且返回了多一行。

要执行严格,您需要返回一个值示例:

max((SELECT DISTINCT(status) AS status_ FROM my_table 
              WHERE EL_F_BDP_PROC_REALTIME.HIVE = 'hive1' 
              AND EXTRACT(MONTH FROM PARTITION_DATE) = EXTRACT(MONTH FROM TO_DATE('20191212', 'YYYY-MM-DD'))
              AND EXTRACT(YEAR FROM PARTITION_DATE) = EXTRACT(YEAR FROM TO_DATE('20191212', 'YYYY-MM-DD'))
            ))

它正在执行,但是,逻辑可能是错误的

尝试一下:

with tbl as (
select 'START' status,     'hive1' str, to_date('2019-12-11', 'yyyy-mm-dd') dt from dual 
union all
select 'NA' status,     'hive1' str, to_date('2019-12-11', 'yyyy-mm-dd') from dual 
union all
select 'END' status,     'hive1' str, to_date('2019-12-11', 'yyyy-mm-dd') from dual)
select dt, str, listagg(status, ' ') WITHIN GROUP (ORDER BY str) 
from tbl
GROUP BY dt, str

答案 1 :(得分:0)

为什么不使用分组依据,

当my_count <= 1 AND status_ ='END'然后为0时选择情况                     当my_count> 1 AND status_ IN('END','RUNNING','START','ERR')THEN 1
                    ELSE -1                结束AS W         INTO return_val from(选择DISTINCT count(status)作为my_count,status作为status_ FROM my_table           在哪里EL_F_BDP_PROC_REALTIME.HIVE ='hive1'           AND EXTRACT(MONTH FROM PARTITION_DATE)= EXTRACT(MONTH FROM TO_DATE('20191212','YYYY-MM-DD'))           AND EXTRACT(YEAR FROM PARTITION_DATE)= EXTRACT(YEAR FROM TO_DATE('20191212','YYYY-MM-DD')) 按状态分组)res;