Last active
December 31, 2015 18:29
-
-
Save momer/8027702 to your computer and use it in GitHub Desktop.
Thank you to davidfetter_disq for helping me reach this conclusion. Thank you as well to johto and ASnyder for offering their help :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH my_vars(intensity_2_duration, intensity_3_duration) AS (VALUES | |
( | |
(SELECT (SUM(duration)*2) FROM users INNER JOIN workouts ON workouts.user_id = users.id | |
WHERE intensity = 3 AND workouts.deleted_at IS NULL), | |
(SELECT SUM(duration) FROM users INNER JOIN workouts ON workouts.user_id = users.id | |
WHERE intensity = 2 AND workouts.deleted_at IS NULL) | |
) | |
) | |
SELECT MAX(date), (SELECT (intensity_2_duration + intensity_3_duration) FROM my_vars) as total_duration, SUM(duration) | |
FROM workouts | |
INNER JOIN users on users.id = workouts.user_id | |
WHERE workouts.deleted_at IS NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I'd like to create a query which selects all of the workouts that belong to a user, which collects the SUM of the duration for each given intensity - adds a multiplier based on the intensity integer, and then returns a single result. | |
That's a weird way to put it - some pseudocode is below: | |
SELECT SUM(intensity_2_duration + intensity_3_duration), workout_date | |
FROM workouts | |
inner join users | |
on user_id = users.id; | |
Where intensity_2_duration & intensity_3_duration would be represented as something like | |
SELECT SUM(duration * 50), workout_date | |
FROM workouts | |
inner join users | |
on user_id = users.id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH my_vars(intensity_2_duration, intensity_3_duration) AS (VALUES | |
( | |
(SELECT (SUM(duration)*2) as intensity_3_duration | |
FROM users | |
INNER JOIN workouts | |
ON workouts.user_id = users.id | |
WHERE | |
workouts.intensity = 2 | |
AND | |
workouts.deleted_at IS NULL | |
AND | |
workouts.date > now()::date - interval '1 weeks' | |
AND | |
users.id = 3), | |
(SELECT (SUM(duration)) as intensity_2_duration | |
FROM users | |
INNER JOIN workouts | |
ON workouts.user_id = users.id | |
WHERE | |
workouts.intensity = 3 | |
AND | |
workouts.deleted_at IS NULL | |
AND | |
workouts.date > now()::date - interval '1 weeks' | |
AND | |
users.id = 3) | |
) | |
) | |
SELECT MAX(date), (SELECT (COALESCE(intensity_2_duration,0) + COALESCE(intensity_3_duration, 0)) FROM my_vars) as total_duration, SUM(duration) | |
FROM workouts | |
INNER JOIN users on users.id = workouts.user_id | |
WHERE | |
workouts.deleted_at IS NULL | |
AND workouts.date > now()::date - interval '1 weeks' | |
AND workouts.user_id = 3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Workouts | |
------------- | |
user_id (integer) | |
intensity (integer) | |
duration (integer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment