Results 1 to 2 of 2

Thread: MAX Group By that displays more columns

  1. #1
    Join Date
    May 2007
    Posts
    3

    MAX Group By that displays more columns

    Basically how can I get around the GROUP BY rule of having column names in both SELECT and GROUP BY clauses for a MAX()?
    I have a query that needs to display two key fields, a field that shows a MAX amount for each category, and also the 2 other supporting fields. My problems is getting the 3 supporting fields to show since GROUP BY only lets me display the aggregate field and the fields used to group. I think some type of JOIN might help but I can't get my head around it. Below is an example of my table.

    Code:
          Table:
    
    NAME           SESSION    SCORE      MINUTES          COLOR
    --------    ----------     ------   --------------          -------
    Tom             1                30            10               Red
    Bob              1               23              3              Blue
    Bob              1               12             5                 Red
    Tom             2                 1             2               Yellow
    Bob             2                8              1               Red
    Bob            2                16              3                Red


    With Name & Session (together) being the key fields, SCORE being what I do MAX() on, and the other two as supporting data and I would like this output

    Code:
    NAME         SESSION        SCORE      TIME         COLOR
    --------    ----------     --------       -------          --------
    TOM            1               30            10                  Red
    Tom             2              1                2               Yellow
    Bob             1              23               3                 Blue
    Bob             2              16               3                Red

  2. #2
    Join Date
    May 2007
    Posts
    3
    ok, I think I figured out... this was the format of the SQL I used:

    Code:
    SELECT DISTINCT [columns i want ultimately displayed]
    FROM [table queried for display] AS a, 
       ( 
         [query to return results of GROUP BY query]
       ) AS c
    WHERE [usual conditions] 
      AND a.[column] = 
        c.[alias of column created by aggregated  function]
    Only problem is that I have to use DISTINCT to not get multiple results (first query times imbedded query). But works for my issue!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •