Oracle SQL - CASE When condition for ANY record Oracle SQL - CASE When condition for ANY record oracle oracle

Oracle SQL - CASE When condition for ANY record


You can use conditional aggregation:

SELECT Doc_Name,        CASE WHEN               COUNT(CASE WHEN Doc_Owner <> Doc_Run_by THEN 1 END) > 0 THEN 'Y'            ELSE 'N'       END AS Share_StatusFROM Log_Table GROUP BY Doc_Name

If there is even a single record (within a Doc_Name group of records) having Doc_Owner <> Doc_Run_by, then Share_Status will be equal to 'Y', otherwise it will be equal to 'N'.

Demo here


It's a dirty trick, but since Y is larger than N, you could take the query you have, group it by the Doc_Name and select the maximum Share_Status:

SELECT   Doc_Name,          MAX (CASE WHEN Doc_Owner <> Doc_Run_by                   THEN 'Y'                   ELSE 'N'              END) as Share_StatusFROM     Log_TableGROUP BY Doc_Name