1 Vote Vote

How can I make this query?

Posted by topdog 287 days ago Questions| query username username point All

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.

  1. Order by sum of user's score on each level.
  2. If user have 2 score on the same level, it will only get the latest score.
  3. Group by username
  4. total score must be less than 10
  5. 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.

Originally asked by: DucDigital on Stack Overflow

Discuss Bury


Who Voted for this Question