This is the table for testing
----------------------------------------------
id | username | point | level | created_date
----------------------------------------------
1 | name_a | 1 | 1 | 2011-08-01
2 | name_a | 2 | 2 | 2011-08-02
3 | name_b | 5 | 1 | 2011-08-02
3 | name_c | 6 | 1 | 2011-08-02
4 | name_d | 1 | 1 | 2011-08-01
5 | name_d | 3 | 1 | 2011-08-02
5 | name_d | 5 | 2 | 2011-08-03
4 | name_e | 5 | 1 | 2011-08-01
5 | name_e | 5 | 2 | 2011-08-02
5 | name_e | 5 | 3 | 2011-08-03
----------------------------------------------
Requirement for the query is to query (as much as possible in one query) the username, point of the table.
- Order by sum of user’s score on each level.
- If user have 2 score on the same level, it will only get the latest score.
- Group by username
- total score must be less than 10
- maximum of the score per level is 5
Output sample:
--------------------
username | tpoint|
--------------------
name_d | 8 |
name_b | 5 |
name_a | 3 |
--------------------
name_e and name_c was ignored.
Sounds like a fun query!
This should do it! Just replace “table”.
EDIT:
Do you want to exclude rows which got a score over 5, or have the value as 5? Just remove WHERE points <= 5 if such.