I'm sorry if this has been asked before, but I just can't find any answer that does the exact thing I need.
My cycling club logs mileage for every ride. To collect data per ride/per rider I have this table (together with a membership table I join to it):
CREATE TABLE `mileage` (
`id` int(11) NOT NULL,
`rideID` mediumint(9) NOT NULL,
`rideDate` date NOT NULL,
`isWeekly` tinyint(1) NOT NULL,
`riderUid` smallint(6) NOT NULL,
`leaderUid` smallint(6) NOT NULL,
`mileage` smallint(6) NOT NULL,
`days` smallint(6) NOT NULL DEFAULT '1',
`eBike` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
To output yearly data I use this MySQL query:
SELECT m.riderUid, w.firstName, w.lastName,
SUM(m.mileage) as miles,
COUNT(*) as cnt,
SUM(m.days) as days
FROM `mileage` m
LEFT JOIN `members` w ON (w.uid = m.riderUid)
WHERE (m.rideDate >= '2018-01-01' AND m.rideDate < '2018-01-01')
GROUP BY m.riderUid
ORDER BY w.lastName;
This outputs totals by rider for the time period.
But I want to get the monthly totals as well, something like this:
Name JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
Rider 1 301 | 213 | 313 | 432 | 556 | 866 | 901 | 877 | 806 | 545 | 512 | 503 |
Rider 2 444 | 525 | 445 | 532 | 636 | 966 | 989 | 900 | 866 | 665 | 633 | 585 |
... etc.
Each row returned from the database will have either an array of monthly data or 12 named (or indexed) months for each row.
I feel like this ought to be simple, but so far it's not. I can easily crunch this in post-processing, but the processing time for that is too great.