I have the following SQL query:
SELECT date(created_at), sum(duration) as total_duration
FROM "workouts" WHERE "workouts"."user_id" = 5 AND "workouts"."category" = 'All'
GROUP BY date(created_at) ORDER BY date(created_at) ASC
but I also want to query for the id of the workout, so I tried this:
SELECT id as id, date(created_at), sum(duration) as total_duration
FROM "workouts" WHERE "workouts"."user_id" = 5 AND "workouts"."category" = 'All'
GROUP BY id, date(created_at) ORDER BY date(created_at) ASC
However, this results in the group by date clause not working (i.e not summing the duration for all workouts on a specific date). I think this is because you cannot have one ID for a date that has multiple records. Is there any way to return the ID even where a specific record returned has multiple workouts associated with it?
For example, if someone had done 3 workouts yesterday, where each lasted 40 minutes in duration, the query would return 120 minutes (sums the durations for a given date) but then also returns each ID for the workouts on that date?
Or should I not do this in the query itself and just do it in the application?
Thanks for any help.