sqlsql-server

Get highest unique number or NULL if none exists


I want biggest possible single number from the table. There are duplicates in the table so I want the final result to reflect null because there is no single biggest number.

I tried every possible modification to my query to have my code return null values but it doesn't. This is leetcode problem that I was trying to solve and trying to build a much deeper understanding of the problem in my SQL query.

Just needed to know how can I introduce NULL values manually. I know the actual coding answer to the question which is why I need some good explanation of what I am doing wrong along with modification to this code.

Thank you for all the help you provide.

Input:

num
8
8
7
7
3
3

Output:

num

Expected:

num
null

Here is the code:

/* Write your T-SQL query statement below */
WITH CTE AS
(
    SELECT *,
    ROW_NUMBER() OVER (PARTITION BY num ORDER BY num) AS RN
    FROM MyNumbers
),
CTE2 AS
(
    SELECT TOP 1 num 
    FROM CTE
    WHERE num NOT IN (SELECT num FROM CTE WHERE RN >= 2)
    ORDER BY num DESC
)
SELECT 
    (CASE
         WHEN num IS NULL OR num = '' 
             THEN NULL
             ELSE num
     END) AS num FROM CTE2;

Solution

  • With your approach, using row_number()

    WITH CTE AS
    (
        SELECT *,
        ROW_NUMBER() OVER (PARTITION BY num ORDER BY num) AS RN
        FROM MyNumbers
    ),
    CTE2 AS
    (
        SELECT TOP 1 num 
        FROM CTE
        WHERE num NOT IN (SELECT num FROM CTE WHERE RN >= 2)
        ORDER BY num DESC
    )
    SELECT 
        max(CASE
             WHEN num IS NULL OR num = '' 
                 THEN NULL
                 ELSE num
         END) AS num FROM CTE2;
    

    If CTE2 not returns rows, max(...) returns null.
    See example with empty table

    create table test3(num int);
    select max(num) maxNum from test3;
    
    maxNum
    null

    fiddle

    If any case, CTE2 returns rows or not, you should calculate max(num).

    Next example:
    You can use count(*)over(...) and filter rows with count=1

    WITH CTE AS
    (
        SELECT *,
          count(*) OVER (PARTITION BY num ORDER BY num) AS cnt
        FROM test MyNumbers
    ),
    CTE2 AS
    (
        SELECT num 
        FROM CTE
        WHERE cnt=1
    )
    select max(num) maxNum from cte2
    

    It is possible to simplify, moving filter (WHERE) to main query

    WITH CTE AS
    (
        SELECT *,
        ROW_NUMBER() OVER (PARTITION BY num ORDER BY num) AS RN
        FROM test MyNumbers
    )
    SELECT max(num) AS num 
    FROM CTE
    WHERE num NOT IN (SELECT num FROM CTE WHERE RN >= 2);
    

    Or

    WITH CTE AS
    (
        SELECT *,
          count(*) OVER (PARTITION BY num ORDER BY num) AS cnt
        FROM test MyNumbers
    )
    select max(num) maxNum 
    from cte
    WHERE cnt=1
    

    Latest:
    directly count and filter rows by HAVING clause

    select max(numg) num
    from( select num numg from test group by num having count(*)=1 )a
    

    Fiddle