Results 1 to 8 of 8

Thread: VB6 SQL Query - Selecting max within a group and other columns

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    7

    VB6 SQL Query - Selecting max within a group and other columns

    Hello,

    I am trying to populate an ADODB Recordset within a VB6 project using an SQL query, but I am encountering difficulties. The simplified version of my problem is as follows. I have a table (tblData) that contains the names, ages, and heights of people, along with other data. I want to populate a recordset with rows that list the person who has the maximum height for each age listed in the table. The relevant columns in the table are called 'Name', 'Age', and 'Height'.

    First, I tried to use GROUP BY to select the maximum height for each age:

    SELECT Age, MAX(Height) AS MaxHeight FROM tblData GROUP BY Age ORDER BY Age

    This worked fine, but when I tried to add the 'Name' column to the SELECT statement, I found that I also had to add the 'Name' column to the GROUP BY statement, which needs to include all the non-aggregate columns. When I did this, I was not able to get the result that I wanted (many extra rows were included in the result of the query).

    I then tried to select all three columns using a correlated subquery:

    SELECT Name, Age, Height FROM tblData AS tblTempData WHERE Height IN (SELECT MAX(Height) FROM tblData WHERE Age = tblTempData.Age) ORDER BY Age

    This actually produces the result that I want, but it is very slow. Does anyone know how I can solve this problem faster, perhaps using a JOIN statement?

    Thank you.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VB6 SQL Query - Selecting max within a group and other columns

    Almost... but it's not quite what you need... what you need to do is like you did in the first query (without the order by).... then join it back to the source table.... sounds complicated, but it isn't .... done this plenty of times before...

    Code:
    SELECT d.Name, d.Age, d.Height
    FROM tblData d
    INNER JOIN (SELECT Age, MAX(Height) AS MaxHeight FROM tblData GROUP BY Age) A
      ON d.Age = A.Age AND d.Height = A.MaxHeight
    The only problem is if you have two people with the same age and height.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: VB6 SQL Query - Selecting max within a group and other columns

    The only problem is if you have two people with the same age and height.
    If you do have that scenario and you only want it to pick one you can introduce a 'top 1' to get around it:-
    Code:
    SELECT d.Name, d.Age, d.Height
    FROM tblData d
    INNER JOIN (Top 1 SELECT Age, MAX(Height) AS MaxHeight FROM tblData GROUP BY Age) A
      ON d.Age = A.Age AND d.Height = A.MaxHeight
    You can't predict which of the 2 matches will be returned but the idea can be extended with an 'order by' if you have a way of defining which should get returned.


    edit> Actually, scratch that. I don't think it'll work when using a joing like that. I usually use sub queries to handle this situation and there you can use it.
    Last edited by FunkyDexter; Apr 18th, 2009 at 02:03 PM.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    7

    Re: VB6 SQL Query - Selecting max within a group and other columns

    Quote Originally Posted by techgnome View Post
    Almost... but it's not quite what you need... what you need to do is like you did in the first query (without the order by).... then join it back to the source table.... sounds complicated, but it isn't .... done this plenty of times before...

    [....]

    The only problem is if you have two people with the same age and height.

    -tg
    Thanks to both of you -- I'll give it a try on Monday and post again if I have problems. I think in my case there will never be two people with the same age and height (I actually used that as a simplified example of the database I'm working with).

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    7

    Re: VB6 SQL Query - Selecting max within a group and other columns

    Quote Originally Posted by techgnome View Post
    Almost... but it's not quite what you need... what you need to do is like you did in the first query (without the order by).... then join it back to the source table.... sounds complicated, but it isn't .... done this plenty of times before...

    Code:
    SELECT d.Name, d.Age, d.Height
    FROM tblData d
    INNER JOIN (SELECT Age, MAX(Height) AS MaxHeight FROM tblData GROUP BY Age) A
      ON d.Age = A.Age AND d.Height = A.MaxHeight
    The only problem is if you have two people with the same age and height.

    -tg
    Thanks a lot -- it seems to work, and it's fast. The only problem is that it complained about the Age column being ambiguous, so I used an alias for that column in the SELECT statement that is the argument of the INNER JOIN:

    Code:
    SELECT d.Name, d.Age, d.Height
    FROM tblData d
    INNER JOIN (SELECT Age AS TmpAge, MAX(Height) AS MaxHeight FROM tblData GROUP BY TmpAge) A
      ON (d.Age = A.TmpAge AND d.Height = A.MaxHeight) ORDER BY d.Age

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    7

    Re: VB6 SQL Query - Selecting max within a group and other columns

    Oops, that should be:

    Code:
    SELECT d.Name, d.Age, d.Height
    FROM tblData d
    INNER JOIN (SELECT Age AS TmpAge, MAX(Height) AS MaxHeight FROM tblData GROUP BY Age) A
      ON (d.Age = A.TmpAge AND d.Height = A.MaxHeight) ORDER BY d.Age
    [/QUOTE]

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VB6 SQL Query - Selecting max within a group and other columns

    it complained about the Age column being ambiguous, so I used an alias for that column in the SELECT statement that is the argument of the INNER JOIN:
    Strange.... I've never had that as an issue before... but if it works, it works....
    Ah... wait... this is in Access isn't it? If so, then that would explain it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    7

    Re: VB6 SQL Query - Selecting max within a group and other columns

    Quote Originally Posted by techgnome View Post
    Strange.... I've never had that as an issue before... but if it works, it works....
    Ah... wait... this is in Access isn't it? If so, then that would explain it.

    -tg
    Ah, yes, I forgot to mention that I'm reading the data out of an Access database.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width