My current query:
Select `userSubUnitsID`, `userSubUnits`.`userID`, CONCAT(`u`.`fname`,' ',`u`.`lname`) AS `full`, `pID`, SUM(`quantComp`) AS `total`
From `userSubUnits`
JOIN `u` ON `u`.`userID` = `userSubUnits`.`userID`
GROUP BY `userID`
What I need is to get this by a date. So it will look more like this.
+-----+------------+----------+-------------+
|user |current week|prev week |two weeks ago|
+-----+------------+----------+-------------+
|John | 564| 354| 687|
+-----+------------+----------+-------------+
|Paul | 451| 328| 845|
+-----+------------+----------+-------------+
etc
Is this possible with a single query? I know how to limit it for the current week but not how to add in the previous weeks.
FYI, Weeks end Sunday at midnight.
Thanks in advance
ugly, but should do the trick. Note that this will fail if your dates span a year boundary. To get around that, you’d have to add in some extra year-based logic.
As well, you’d be better off doing this kind of transformation client-side. It’s ugly to expand, and will very quickly get hideously inefficient if you need to start doing more than just a few weeks, e.g: