sqlsnowflake-cloud-data-platform

Select status with if-else condition in SQL


Currently working on Snowflake. I want to write a SQL query for the following table:

pipeline status
MONTHROPE NoDelta
MONTHROPE NoDataFound
MONTHROPE NoFiles
DAYROPE NoDataFound
FIVEROPE NoDelta
DAYROPE Failed
FIVEROPE NoDelta
ANNUAL NoDataFound

Requirement is to select one pipeline (after grouping) with a status of certain priority.

i.e. if NoDelta is present then it should be selected as it is first priority. else-if NoDataFound is second priority.

Priority for select is NoDelta, NoDataFound, NoFiles, Failed.

Hence, the result should be as follows:

pipeline status
MONTHROPE NoDelta
DAYROPE NoDataFound
FIVEROPE NoDelta
ANNUAL NoDataFound

Can you please help me form the query? I am trying to use CASE, but do not know the correct order.


Solution

  • CASE assigns priority and QUALIFY selects the status with the highest priority for each pipeline.

    SELECT
        pipeline,
        status
    FROM table
    QUALIFY ROW_NUMBER() OVER (
        PARTITION BY pipeline
        ORDER BY CASE status
            WHEN 'NoDelta' THEN 1
            WHEN 'NoDataFound' THEN 2
            WHEN 'NoFiles' THEN 3
            WHEN 'Failed' THEN 4
            ELSE 5
        END ASC
    ) = 1
    ORDER BY pipeline;