Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2012
    Posts
    44

    Unanswered: Query help: Get the biggest win

    I want to get the biggest win from a table. Two columns is needed here, homeScore and awayScore.

    This is what I have come up with so far:

    Code:
    SELECT *
    FROM table
    ORDER BY (homeScore-awayScore) DESC, awayScore DESC LIMIT 1
    Of course, this will only get the biggest home win. How could I include the calculation of the biggest away win as well?

    Thanks for any kind of help and sorry if it's a stupid question.
    Last edited by Haydn; 02-20-12 at 05:35. Reason: Forgot the limit

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    ORDER BY GREATEST(homeScore-awayScore,awayScore-homeScore) DESC
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Another alternative (although I do love Rudy's solution!)
    Code:
    ORDER
        BY Abs(homescore - awayscore) DESC
    MySQL :: MySQL 5.0 Reference Manual :: 11.6.2 Mathematical Functions
    George
    Home | Blog

  4. #4
    Join Date
    Feb 2012
    Posts
    44
    Quote Originally Posted by r937 View Post
    ORDER BY GREATEST(homeScore-awayScore,awayScore-homeScore) DESC
    Thanks a lot, appreciate it.

    Edit: I didn't notice your post there gvee, thanks for that one as well!

    Although I have one extra question here. If the difference of two results is the same, but the winning result have more goals, I would like the latter to be sorted first.

    Example:
    6-1
    5-0

    The 6-1 result should be sorted before the 5-0....is that a problem?
    Last edited by Haydn; 02-20-12 at 06:59.

  5. #5
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Essentially you want the game with the highest total score first:
    Code:
    ORDER
        BY Abs(homescore - awayscore) DESC
         , homescore + awayscore DESC
    George
    Home | Blog

  6. #6
    Join Date
    Feb 2012
    Posts
    44
    Quote Originally Posted by gvee View Post
    Essentially you want the game with the highest total score first:
    Code:
    ORDER
        BY Abs(homescore - awayscore) DESC
         , homescore + awayscore DESC
    Of course, thanks!

    Your variant with the Abs was clever indeed.

    I think I'm falling in love with this website before 24 hours has passed since I registered.

Posting Permissions

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