简体   繁体   中英

Nested case statement in Oracle

I have been staring at this for the last couple of hours and I don't see where I'm throwing off this query, the code has been anonymized so forgive the replacement identifiers:

select count (distinct(case when status not in ('Larry','Curly','Moe','Shemp')
                       then case when(case when 
                       (MIN(case when status in ('Larry','Curly','Moe','Shemp')
                            then case when to_char(my_date,'YYYY') = 1900
                            then my_dttm
                            else my_dttm
                            end
                            end) <= '18-nov-2019')
            and
                       (MIN(case when status in ('Larry','Curly','Moe','Shemp')
                            then case when to_char(my_date,'YYYY') = 1900
                            then my_dttm
                            else my_dttm
                            end
                            end) >= '1-oct-2019')
            then
                        MIN(case when status in ('Larry','Curly','Moe','Shemp')
                            then case when to_char(my_date,'YYYY') = 1900
                            then my_dttm
                            else my_dttm
                            end
                            end)end) is null
           then case when to_date >= '1-oct-2019' and from_date <= '18-nov-2019' then c_id end
)) from my_table; -- error code references this line

The error code I'm getting : ORA-00905: missing keyword 00905. 00000 - "missing keyword". I searched this site for something similar but I haven't been able to find anything that fits my specific situation. Help would be greatly appreciated. Thanks

You are missing two END keywords at error line.

See the following:

SELECT
    COUNT(DISTINCT(CASE
        WHEN STATUS NOT IN(
            'Larry', 'Curly', 'Moe', 'Shemp'
        ) THEN CASE
            WHEN(CASE
                WHEN(MIN(CASE
                    WHEN STATUS IN(
                        'Larry', 'Curly', 'Moe', 'Shemp'
                    ) THEN CASE
                        WHEN TO_CHAR(MY_DATE, 'YYYY') = 1900 THEN MY_DTTM
                        ELSE MY_DTTM
                    END
                END) <= '18-nov-2019')
                    AND(MIN(CASE
                    WHEN STATUS IN(
                        'Larry', 'Curly', 'Moe', 'Shemp'
                    ) THEN CASE
                        WHEN TO_CHAR(MY_DATE, 'YYYY') = 1900 THEN MY_DTTM
                        ELSE MY_DTTM
                    END
                END) >= '1-oct-2019') THEN MIN(CASE
                    WHEN STATUS IN(
                        'Larry', 'Curly', 'Moe', 'Shemp'
                    ) THEN CASE
                        WHEN TO_CHAR(MY_DATE, 'YYYY') = 1900 THEN MY_DTTM
                        ELSE MY_DTTM
                    END
                END)
            END) IS NULL THEN CASE
                WHEN TO_DATE >= '1-oct-2019'
                     AND FROM_DATE <= '18-nov-2019' THEN C_ID
            END -- this is missing in your code
        END -- this is missing in your code
    END))
FROM
    MY_TABLE;

Cheers!!

Following two case statements didn't have "end"

select count (distinct(case when status not in ('Larry','Curly','Moe','Shemp') then case when(case when (MIN(case when status in ('Larry','Curly','Moe','Shemp') then case when to_char(my_date,'YYYY') = 1900 then my_dttm else my_dttm end end) <= '18-nov-2019') and (MIN(case when status in ('Larry','Curly','Moe','Shemp') then case when to_char(my_date,'YYYY') = 1900 then my_dttm else my_dttm end end) >= '1-oct-2019') then MIN(case when status in ('Larry','Curly','Moe','Shemp') then case when to_char(my_date,'YYYY') = 1900 then my_dttm else my_dttm end end)end) is null then case when to_date >= '1-oct-2019' and from_date <= '18-nov-2019' then c_id end end end )) from my_table;

case when status not in ('Larry','Curly','Moe','Shemp')
then case when(case when 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM