Lakshya
Lakshya

Reputation: 506

Get MAX Value IN SQL After SUM

I am having a table in SQL which is containing 2 points for 1 student_id in one match like in below Photoenter image description here

Now Like this there are many Student_profile_ID and Basketball_Match_ID. And I want to make the Student_profile_ID same and then add the points after That want to Get Who Score the Maximum point among them.

I am Able to Sum then But NOt able to GET the MAX value

SELECT student_profile_id,
       sum(point) as total_point 
from `basketball_points` 
where student_profile_id = {$value2} 
  AND basketball_match_id = {$key3}

Where Foreach Loop help me to increment the Value In PHP. And check all the results

Upvotes: 2

Views: 199

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269573

I think you want group by along with order by and limit. Here is the idea:

select student_profile_id, sum(point) as total_point
from `basketball_points`
where basketball_match_id = {$key3}
group by student_profile_id
order by total_point desc
limit 1;

Note: this will only give you one student if multiple students are tied for the maximum. Also, it is unclear whether or not you want the where clause.

If you want the total points for all students, leave out the limit clause.

Upvotes: 3

Alex Tartan
Alex Tartan

Reputation: 6826

Just use:

SELECT 
    student_profile_id, 
    max(point) as max_point,
    sum(point) as total_points
FROM
    `basketball_points` 
WHERE student_profile_id = {$value2} AND basketball_match_id = {$key3}

Upvotes: 2

Related Questions