mysql - SELECT specific rows when using GROUP BY -


I have the following SQL table that tracks the user's score at a specific time. The user can have several digits per day.

  + ------- + ------------ + ------- + - ---- + | User | Date | Score | ... | + ------- + ------------ + ------- + ----- + | Bob | 2014-04-19 | 100 | ... | | Mary | | 2014-04-19 | 100 | ... | | Alice | 2014-04-20 | 100 | ... | | Bob | 2014-04-20 | 110 | ... | | Bob | 2014-04-20 | 125 | ... | | Mary | 2014-04-20 | 105 | ... | | Bob | 2014-04-21 | 115 | ... | + ------- + ------------ + ------- + ----- +   

A special form Users given (say bob ), how do I prepare a report for each user's score, but should I use the highest rated score per day? (Getting the specific line with the highest score is also important with the highest score)

  SELECT * FROM from `user_score` to WHERE 'user` =' bob 'by group' Date`   

The base query that I am closing turns the result into the following result set:

  + ------- + ------------ + ------- + - --- + | User | Date | Score | ... | + ------- + ------------ + ------- + ----- + | Bob | 2014-04-19 | 100 | ... | | Bob | 2014-04-20 | 110 | ... | | Bob | 2014-04-21 | 115 | ... | + ------- + ------------ + ------- + ----- +   

The high score of 125 to 2014-04-20 is missing. I tried to make sure that `user_score` from maximum SELECT *, MAX (score) with the maximum (score) where 'user' = 'bob' The group returns the highest score for the day, but it is not the row in which the highest score is the other column values ​​of that row are important,

 < code> + ------- + ------------ + ------- + - ---- + ------------ + | User | Date | Score | ... | Maximum (score) | + ------- + ------------ + ------- + ----- + ------------ + | Bob | 2014-04-19 | 100 | ... | 100 | | Bob | 2014-04-20 | 110 | ... | 125 | | Bob | 2014-04-21 | 115 | ... | 110 | + ------- + ------------ + ------- + ----- + ------------ + < Finally, I tried  
  SELECT *, MAX (score) with 'user_score` where' user '=' bob 'and score = maximum (points) group' date`   

but the result is in invalid use of GROUP BY .

  • Is on the right track, am I trying to complete, but I do not know the specific score to filter.

    Edit:

    SQLFiddle:

    < p> If you want all the fields, then the easiest (faster way) in MySQL is not present :
      select 'user_score' to 'us' = 'Bob' is not (select 1 from user_scrow where we have us 2. 'user' = us.` user 'and us2.date = us .date and us2.score & gt; us.score);   

    It may look like a strange perspective and, I agree that it is. What it's doing, it's very easy: "Get all the rows from me user_score for Bob where there is no high score (for Bob)". This is equal to getting the line with the maximum score. With an index on user_score (name, score) , this is probably the most effective way of doing what you want.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -