I have a mysql table that has columns for 'created_at' and 'type', among others. What I am trying to do is get the total number of rows created each week, and further have that number broken down by 'type'.
That is, if my data is as follows:
created_at | type
10/15/2015 car
10/15/2015 bike
10/16/2015 car
10/24/2015 car
10/24/2015 car
10/25/2015 car
10/26/2015 bike
I want my query to return something like:
Week of | Total | car | bike
10/15/2015 3 2 1
10/22/2015 4 3 1
From this stackoverflow question (How to group by week in MySQL?), I am able to group by week, but I am having difficulty further breaking down the results by 'type'. The closest I have gotten is:
SELECT
FROM_DAYS(TO_DAYS(created_at) -MOD(TO_DAYS(created_at) -1, 7)) AS week_of,
type,
COUNT(*) AS 'rows'
FROM mytable
GROUP BY FROM_DAYS(TO_DAYS(created_at) -MOD(TO_DAYS(created_at) -1, 7)), type
ORDER BY FROM_DAYS(TO_DAYS(created_at) -MOD(TO_DAYS(created_at) -1, 7));
Which is a bit different then what I was trying to get, but its something I can still work with. However, it doesn't include the total per week (would prefer having all the data I want returned in the result set). How would I go about doing this?
Since I don't know the values in 'type', I don't want to specify those in the query directly, especially since those can be changed over time.