If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > SUM/ MAX query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-15-11, 22:13
Julian Mishke Julian Mishke is offline
Registered User
 
Join Date: Mar 2011
Posts: 4
SUM/ MAX query

Hello,

I am very new to this and having problems with the SUM and MAX in my query:


WHERE points = (SELECT MAX(points) FROM Score WHERE roundName = '***') AND roundName = '***'


I will greatly appreciate any help.

Last edited by Julian Mishke; 03-16-11 at 18:34.
Reply With Quote
  #2 (permalink)  
Old 03-16-11, 04:21
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
Hi,

you mention "the person who got the highest scores as a sum of the 5 scores in the final round"

So I think this statement is not strictly correct:

SELECT MAX(points) FROM Score WHERE roundName = 'Final'

Try something like

Code:
SELECT Max(performerpoints)
FROM   (SELECT SUM(points) AS performerpoints
        FROM   score
        WHERE  roundname = 'Final'
        GROUP  BY performerid)
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 03-16-11, 06:35
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Code:
SELECT Performer.performerName
     , Country.countryName
     , SUM(Score.points) AS total_score
  FROM Event
INNER
  JOIN Score
    ON Score.eventId = Event.eventId
   AND Score.roundName = 'Final'
INNER
  JOIN Performer
    ON Performer.performerId = Score.performerId
INNER
  JOIN Country
    ON Country.countryCode = Performer.countryCode
 WHERE Event.eventName = 'Piano'
GROUP
    BY Performer.performerId
ORDER
    BY total_score DESC LIMIT 1
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 03-16-11, 16:54
Julian Mishke Julian Mishke is offline
Registered User
 
Join Date: Mar 2011
Posts: 4
MAX points

Thank you both very much!

Rudy, your query works like a dream the only problem is- I have few winners. Would it be possible to remove the limit and just get it to show any number of highest scorers?

I tried removing the LIMIT 1 and it's just showing me the list of all the performers in the round of the event. If it was only that simple...

Big thank you in advance!

Last edited by Julian Mishke; 03-16-11 at 18:34.
Reply With Quote
  #5 (permalink)  
Old 03-16-11, 17:01
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
Try the following:
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com

Last edited by it-iss.com; 03-16-11 at 17:06. Reason: The code that I originally posted was not correct.
Reply With Quote
  #6 (permalink)  
Old 03-16-11, 17:08
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
ronan, looks like you forgot the 'Piano' event condition

Code:
SELECT Performer.performerName
     , Country.countryName
     , SUM(Score.points) AS total_score
  FROM Event
INNER
  JOIN Score
    ON Score.eventId = Event.eventId
   AND Score.roundName = 'Final'
INNER
  JOIN Performer
    ON Performer.performerId = Score.performerId
INNER
  JOIN Country
    ON Country.countryCode = Performer.countryCode
 WHERE Event.eventName = 'Piano'
GROUP
    BY Performer.performerId
HAVING total_score =
       ( SELECT MAX(subtotal)
           FROM ( SELECT Score.performerId
                       , SUM(Score.points) AS subtotal
                    FROM Event
                  INNER
                    JOIN Score
                      ON Score.eventId = Event.eventId
                     AND Score.roundName = 'Final'
                   WHERE Event.eventName = 'Piano'
                  GROUP
                      BY Score.performerId ) AS t )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 03-16-11, 17:14
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
Rudy as much as I think I know SQL you are just on another level!!
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #8 (permalink)  
Old 03-16-11, 17:32
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
thanks ronan, appreciate it

you're not so bad yourself, babe

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 03-16-11, 18:00
Julian Mishke Julian Mishke is offline
Registered User
 
Join Date: Mar 2011
Posts: 4
Thank you soooo very much! You both are Real Masters! Hats off!
I would love to learn from you.

I am trying to understand your code Rudy. Could I please, ask you:

why are you starting from Event table (right after the first SELECT command)?
And what does ‘t’ mean at the end?

Massive 'Thank You' for all your time and advice!

Last edited by Julian Mishke; 03-17-11 at 15:22.
Reply With Quote
  #10 (permalink)  
Old 03-16-11, 18:22
Julian Mishke Julian Mishke is offline
Registered User
 
Join Date: Mar 2011
Posts: 4
*** Thank you!!!

Last edited by Julian Mishke; 03-17-11 at 15:22.
Reply With Quote
  #11 (permalink)  
Old 03-16-11, 18:26
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by Julian Mishke View Post
why are you starting from Event table
because that's likely the "driving" table which the optimizer will pick as the most restrictive table (see the WHERE clause) -- thus returning fewer rows to be joined, as opposed to starting with, say, the score table, returning all scores and then throwing away the ones that aren't for the right event

Quote:
Originally Posted by Julian Mishke View Post
And what does ‘t’ mean at the end?
it's a table alias and is required for subqueries in the FROM clause
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On