0

I am trying to create a query that will output a spreadsheet of values over a certain date range. The desired output is shown below:

tab    
+---------------+--------+--------+--------------+
| date          | userID | meters | totalSeconds |
+---------------+--------+--------+--------------+
| 2021-11-01    | 14     | 48000  | 12415.8      |
| 2021-11-02    | 14     | 0      | 0            |
| 2021-11-03    | 14     | 21204  | 5040         |
| 2021-11-04    | 14     | 0      | 0            |
| 2021-11-05    | 14     | 26124  | 6101.1       |
+---------------+--------+--------+--------------+

This is the query that I am currently running:

SELECT gen_date as date, 
COALESCE(userIDUserID, userIDUserID) AS userID, 
COALESCE(SUM(meters), 0) AS meters, 
COALESCE(SUM(totalSeconds), 0) AS totalSeconds from
(select adddate('2020-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) gen_date from
 (select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) as gd
LEFT JOIN prod.workout_log ON DATE(prod.workout_log.datetime) = gd.gen_date
WHERE gen_date between '2021-06-01' and '2021-12-31' AND prod.workout_log.userIDUserID = 14
GROUP BY gen_date, userID
ORDER BY userID;

And this is the output I'm currently receiving:

tab    
+---------------+--------+--------+--------------+
| date          | userID | meters | totalSeconds |
+---------------+--------+--------+--------------+
| 2021-11-01    | 14     | 48000  | 12415.8      |
| 2021-11-03    | 14     | 21204  | 5040         |
| 2021-11-05    | 14     | 26124  | 6101.1       |
+---------------+--------+--------+--------------+

I have looked through a few threads similar to this question to get to this point, but nothing I have tried has helped me to achieve my exact use case. Can anybody point me in the right direction? Thank you!

palfred
  • 31
  • 3
  • Move `AND prod.workout_log.userIDUserID = 14` to the `ON` clause. – Barmar Feb 10 '22 at 21:28
  • You've got a complicated query for the data you're looking for could you give the tables you're querying? –  Feb 10 '22 at 21:32

0 Answers0