Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Select identical rows based on max values of a column

    I have a table named 'demo' with the following data:

    Name Group MX
    A XY 1
    B YZ 1
    B XY 2
    C YZ 5
    C XY 3
    D YZ 2
    E YZ 1
    E XY 1

    I want unique names based on 'MX' maximum value, when the 'MX' for two identical names are equal I need one of them as illustrated below:
    Name Group MX
    A XY 1
    B XY 2
    C YZ 5
    D YZ 2
    E YZ 1 -- or this E XY 1

    This is my query and it is working charmingly, but when the two names are identical it displays both as follow:
    Name Group MX
    A XY 1
    B XY 2
    C YZ 5
    D YZ 2
    E YZ 1
    E XY 1


    Best,

  2. #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    North Carolina
    Posts
    1,492
    Thanks
    4
    Thanked 235 Times in 232 Posts
    I think you may have forgotten to post your query.
    Dave .... HostMonster for all of your hosting needs

  3. #3
    New to the CF scene
    Join Date
    Sep 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is the SQL query:
    Code:
    SELECT demo.Name, demo.Group, demo.MX
      FROM ( SELECT Name, MAX(MX) AS max_values FROM demo
             GROUP BY Name) demo2
    INNER JOIN demo
        ON demo.Name = demo2.Name
       AND demo.MX = demo2.max_values


 

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
  •