I have this table with rows of training activities:
id training_date training_activity
1 2009-01-02 running
2 2009-01-03 cycling
3 2009-01-04 cycling
4 2009-01-07 running
5 2009-01-10 running
6 2009-01-12 cycling
7 2009-01-14 running
8 2009-01-15 fitness
9 2009-01-16 running
10 2009-01-20 running
What I want to extract from it is some sort of summary, like this:
wk # run # cycling # fitness
1 2 2 0
2 2 1 0
3 2 0 1
or, in PHP, an array with rows for each week and counts for the different training activities
so far, I have come to this
SELECT
date_format(`training_date`, '%U') as Week,
`training_activity` as Training,
ifnull(count(`training_activity`),0) as Amount
FROM `trainings`
WHERE `user_id` = 1
GROUP BY
`training_activity`,
date_format(`training_date`, '%U'),
date_format(`training_date`, '%Y')
ORDER BY
date_format(`training_date`, '%Y') DESC,
date_format(`training_date`, '%U') DESC
LIMIT 52
However, with a query like this I end up with a result like:
wk 1 running 3
wk1 cycling 2
etc
Not what I want. Maybe I need to do something with inner joins. Any hints?