dbTalk Databases Forums  

get sum() grouped max() of group

comp.databases.mysql comp.databases.mysql


Discuss get sum() grouped max() of group in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jeff Carpereret
 
Posts: n/a

Default get sum() grouped max() of group - 07-02-2011 , 04:06 AM






hello,

I have table structure like below. Each row is one played game, each
person can play many or none times in each month.

id | person | score | date |
------------------------------------
1 | 32 | 444 | 2011-05 |
2 | 65 | 528 | 2011-05 |
3 | 77 | 455 | 2011-05 |
4 | 32 | 266 | 2011-06 |
5 | 77 | 100 | 2011-06 |
6 | 77 | 457 | 2011-06 |
7 | 77 | 457 | 2011-06 |
8 | 65 | 999 | 2011-07 |
9 | 32 | 222 | 2011-07 |
I am trying to get for each person sum of its best score in each
month. S result of above should be:

person | SUM(ofbestofeachmonth)
---------------------------------
32 | 932
65 | 1527
77 | 912
I know how to fetch the bests scores per userin month or some range


SELECT person, date, MAX(score) as 'sc' FROM tabgames WHERE
MONTH(date) = 6 GROUP BY person HAVING (sc>0)


Because i need in the end results of sum of max at quarter of year,
now i am fetching best for each month and outside the MySQL i am
adding them.

Now i am reading about group-wise max and still try to get excpected
results. Any help

TIA

Also on http://stackoverflow.com/questions/6...d-max-of-group

Reply With Quote
  #2  
Old   
Axel Schwenke
 
Posts: n/a

Default Re: get sum() grouped max() of group - 07-02-2011 , 05:00 AM






Jeff Carpereret <carpereret (AT) gmail (DOT) com> wrote:
Quote:
I have table structure like below. Each row is one played game, each
person can play many or none times in each month.

id | person | score | date |
------------------------------------
1 | 32 | 444 | 2011-05 |
2 | 65 | 528 | 2011-05 |
3 | 77 | 455 | 2011-05 |
4 | 32 | 266 | 2011-06 |
5 | 77 | 100 | 2011-06 |
6 | 77 | 457 | 2011-06 |
7 | 77 | 457 | 2011-06 |
8 | 65 | 999 | 2011-07 |
9 | 32 | 222 | 2011-07 |

I am trying to get for each person sum of its best score in each
month.

SELECT person, date, MAX(score) as 'sc' FROM tabgames WHERE
MONTH(date) = 6 GROUP BY person HAVING (sc>0)
That's a start. But why limit it to one month?

SELECT person, date, MAX(score) as 'sc'
FROM tabgames GROUP BY person, date

Quote:
Because i need in the end results of sum of max at quarter of year,
If you want that data for only one quarter then add a condition

WHERE date BETWEEN <first-month-of-quarter> AND <last-month-of-quarter>

Quote:
Now i am reading about group-wise max and still try to get excpected
results.
This is not the groupwise maximum problem.

Simply use a derived table subquery based on the above query:

SELECT person, SUM(sc) FROM
( SELECT person, MAX(score) AS 'sc'
FROM tabgames GROUP BY person, date
) AS t1
GROUP BY person

http://dev.mysql.com/doc/refman/5.1/...ubqueries.html


XL

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.