Search code examples
sqloracle-databasecaseoracle12c

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


Solution

  • 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!!