• A while back, I promised to post the QotD code in a copy/paste ready format in the discussion when I have the code as a graphic in the question. Yet, I always forget to do as I promised. My apologies to all those who already spent time typing it in for yourself.

    And to those who want to try it out, and experiment to see how small changes affect the result, here is some SQL you can copy and paste:

    CREATE TABLE QotD

    (GroupName varchar(20) NOT NULL,

    TheValue int NOT NULL,

    PRIMARY KEY (GroupName, TheValue)

    );

    go

    INSERT INTO QotD (GroupName, TheValue)

    SELECT 'Group 1', 1 UNION ALL

    SELECT 'Group 1', 2 UNION ALL

    SELECT 'Group 1', 3 UNION ALL

    SELECT 'Group 2', 1 UNION ALL

    SELECT 'Group 2', 2 UNION ALL

    SELECT 'Group 3', 1 UNION ALL

    SELECT 'Group 3', 3 UNION ALL

    SELECT 'Group 4', 2 UNION ALL

    SELECT 'Group 4', 3 UNION ALL

    SELECT 'Group 5', 1 UNION ALL

    SELECT 'Group 6', 2 UNION ALL

    SELECT 'Group 7', 3;

    go

    SELECT a.GroupName --, MAX(a.TheValue)

    FROM QotD AS a

    GROUP BY a.GroupName

    HAVING EXISTS (SELECT *

    FROM QotD AS b

    WHERE b.TheValue > MAX(a.TheValue));

    go

    DROP TABLE QotD;

    go

    EDIT: I added an extra column, commented out, to the SELECT. This might help you see what happens.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/