Search code examples
sqlselectdistinct

SQL Find unique items AND one of the column value is NULL


I have a table w/ the format like below:

ItemNo  ItemDesc
11111   Item01 Description
11111   Item01 Description2
11111   <NULL>   
22222   <NULL>    
33333   Item03 Description
33333   Item03 Description2
33333   <NULL>

I'd like to find the unique item 22222. TIA.


Solution

  • SELECT
        ItemNo
    FROM
        yourTable
    GROUP BY
        ItemNo
    HAVING
            COUNT(*) = 1
        AND SUM(CASE WHEN ItemDesc IS NULL THEN 1 END) = 1