I know enough MySQL to do simple joins but I’m lacking the knowledge when it comes to anything advanced. I know that I could strip out the rows in PHP but I believe that getting the query right is cleaner.
What I’m currently doing is:
Query:
SELECT DAYOFWEEK(r.`date`) AS weekNumber, r.`date`AS scoreDate, qf.target, r.totalScore FROM results;
Result:
weekNumber, scoreDate, target, totalScore
4, '2012-02-29', 85, 100
3, '2012-03-06', 85, 100
3, '2012-03-06', 85, 75
3, '2012-03-06', 85, 42
Is it possible to:
- group by weekNumber,
- change the score date to show a ‘from and to date’ ,
- sum up the target ‘s and divide by the number of rows grouped together
- and the do the same for totalScore as we did with the target’s?
The outcome that I would like from the query is:
weekNumber, scoreDate, target, totalScore
4, '2012-02-29 to 2012-02-29', 85, 100
3, '2012-03-06 to 2012-03-06', 85, 72.3
Can someone help me create the query or tell me how I can produce the result that I’m looking for?
Thanks!
the query above should do what you want. just change the q1 – q3 whit your fields. and q with you table name.
outcome:
Database:
source:
http://www.w3schools.com/sql/sql_func_avg.asp