• Robert Frasca (9/8/2010)


    ..., there can never be anything GREATER THAN a?...

    Robert, I believe the key is to treat the groups individually. Therefore, when Group 1 from "a" is being evaluated (because of the group by), it is being evaluated against the whole QotD table "b".

    Here, we are looking for the GroupNames from the grouped set "a" whose MAX(a.TheValue) values are less than the "b.TheValue"

    Since the MAX(a.TheValue) for Group 1 is 3, which is same as the max value for "b" it does not appear in the result set.

    The MAX(a.TheValue) for Group 2 is 2, which is less than "b", and therefore it appears in the result set.

    Hope that clears things out a bit. You can break the query into two, with the first part being:

    SELECT a.GroupName, MAX(a.TheValue)

    FROM QotD AS a

    GROUP BY a.GroupName, a.TheValue

    Now compare this with "b" - you should start getting a feel of the internal workings.

    Thanks & Regards,
    Nakul Vachhrajani.
    http://nakulvachhrajani.com

    Follow me on
    Twitter: @sqltwins