Results 1 to 4 of 4

Thread: Getting Max row value in SQL

  1. #1
    Join Date
    Jun 2012
    Location
    Iran , Tehran
    Posts
    93
    Thanks
    5
    Platforms
    Unix/X11 Windows Android

    Smile Getting Max row value in SQL

    hi all
    i have a table in my data base that have the following schema:
    mytable ( id , value)
    like this:
    id value
    1 4
    2 7
    3 10
    4 1
    5 20

    need to fetch 'id' of the row that have the max value in 'value' i try this query (and worked) :
    SELECT R.id
    FROM (SELECT id , MAX(value)
    FROM mytable
    GROUP BY id) as R(id,mymax)

    my question : is there any better way to doing this work ? if so,how?
    Life is about making the right decisions and moving on.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting Max row value in SQL

    Hmmm...

    "SELECT id, MAX(value) FROM mytable;"?

    alternatively

    "SELECT id FROM mytable ORDER BY value DESC LIMIT 1"
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2012
    Location
    Iran , Tehran
    Posts
    93
    Thanks
    5
    Platforms
    Unix/X11 Windows Android

    Default Re: Getting Max row value in SQL

    thanks Mr wysota for replying.

    please explain to me "LIMIT 1" what will do? i'm using Microsoft SQL Server 2008 (it seems the sql server not support this clause)

    thanks

    -- solved i must use TOP clause in SQL Server ! thanks again Mr Wysota !
    Last edited by Ali Reza; 8th December 2012 at 07:36. Reason: updated contents
    Life is about making the right decisions and moving on.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Getting Max row value in SQL

    Neither TOP nor LIMIT is portable if that is a concern. You can do it with a sub-select, and this should work everywhere (until someone finds an SQL engine that doesn't).

    Be careful, you could have several rows with the same maximum value. Which one would you want in that case?
    Qt Code:
    1. > select id, value from test;
    2. 1|1
    3. 2|2
    4. 3|3
    5. 4|31
    6. 5|31
    7. 6|32
    8. 7|44
    9. 8|44
    10.  
    11. > select id from test where value = (select max(value) from test);
    12. 7
    13. 8
    14. > select max(a) from test where b = (select max(b) from test);
    15. 8
    16. > select min(a) from test where b = (select max(b) from test);
    17. 7
    To copy to clipboard, switch view to plain text mode 
    For large row counts an index on value would be a good idea.

  5. The following user says thank you to ChrisW67 for this useful post:

    Ali Reza (6th January 2013)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.