我有4个表:用户,锻炼,练习和结果。 “用户”发布“练习”的“结果”,其链接到单个“锻炼”。但是当用户发布结果时,由于有多个练习,一个练习的结果可以与唯一的“post_id”链接。我想知道用户根据他们提供了多少“post_ids”来运用的总分钟数,这些“post_ids”可以链接到“Workouts”表,其中“workout_length”列显示每个锻炼持续多少分钟。我还要加上他们的总卡路里......卡路里存储在“练习”表中。这里有一些样本数据,在这种情况下,锻炼(workout_id = 1)有两个练习,锻炼长度为1分钟......每个锻炼每个锻炼消耗1卡路里。
结果:
user_id| workout_id| post_id| exercise_id| number_of_reps|
-------+-----------+--------+------------+---------------+
123| 1 | 1| 1 | 18|
123| 1 | 1| 2 | 29|
123| 1 | 2| 1 | 15|
123| 1 | 2| 2 | 30|
123| 1 | 3| 1 | 20|
123| 1 | 3| 2 | 28|
-------+-----------+--------+------------+---------------+
锻炼:
workout_id| workout_length|
----------+---------------+
1| 1|
----------+---------------+
锻炼:
workout_id| exercise_id| calories|
----------+------------+---------+
1| 1| 1|
1| 2| 1|
----------+------------+---------+
用户:
user_id| first_name| last_name|
-------+-----------+----------+
123| John| Doe|
-------+-----------+----------+
我能够成功生成两个单独的表...一个用于总计总分钟,另一个用于总计卡路里总和;但是,我需要在一个表和GROUP BY用户中获得这些总数。以下是我使用的查询:
总卡路里:
SELECT SUM(calories * reps) as total_cals, user_id, first_name, last_name
FROM results
LEFT OUTER JOIN exercises on results.exercise_id = exercises.id
LEFT OUTER JOIN workouts on results.workout_id = workouts.id
LEFT OUTER JOIN users on results.user_id = users.id
GROUP BY user_id, first_name, last_name
ORDER BY total_cals DESC
总分钟数:
SELECT SUM(workout_length) as total_mins, first_name, last_name, user_id
FROM
(SELECT DISTINCT(results.post_id), users.first_name, users.last_name, workouts.workout_length
FROM results
LEFT OUTER JOIN users on results.user_id = users.id
LEFT OUTER JOIN workouts ON results.workout_id = workouts.id)
GROUP BY first_name, last_name, user_id
这两个查询都可以单独运行,但是当我尝试使用WITH语句将它们组合成一个查询时,SQLite3会抛出一个错误,指出“workout_length列不存在”。我知道要看很多东西......但是有什么想法吗?
WITH minute_table AS (SELECT SUM(workouts.workout_length) as total_mins, user_id
FROM
(SELECT DISTINCT(results.post_id), users.id
FROM results
LEFT OUTER JOIN users on results.user_id = users.id
LEFT OUTER JOIN workouts ON results.workout_id = workouts.id)
GROUP BY user_id),
cals_table AS
(SELECT SUM(calories * reps) as total_cals, user_id, first_name, last_name
FROM results
LEFT OUTER JOIN exercises ON results.exercise_id = exercises.id
LEFT OUTER JOIN workouts ON results.workout_id = workouts.id
LEFT OUTER JOIN users on results.user_id = users.id
GROUP BY user_id, first_name, last_name)
SELECT cals_table.first_name, cals_table.last_name, cals_table.total_cals, minute_table.total_mins
FROM minute_table
JOIN cals_table ON minute_table.user_id = cals_table.user_id
答案 0 :(得分:0)
我想通了......只需要添加" workouts.workout_length"到第一个SELECT语句。
WITH minute_table AS (SELECT SUM(workout_length) as total_mins, user_id
FROM
(SELECT DISTINCT(results.post_id), workouts.workout_length, users.id as user_id
FROM results
LEFT OUTER JOIN users on results.user_id = users.id
LEFT OUTER JOIN workouts ON results.workout_id = workouts.id)
GROUP BY user_id),
cals_table AS
(SELECT SUM(calories * reps) as total_cals, user_id, first_name, last_name
FROM results
LEFT OUTER JOIN exercises ON results.exercise_id = exercises.id
LEFT OUTER JOIN workouts ON results.workout_id = workouts.id
LEFT OUTER JOIN users on results.user_id = users.id
GROUP BY user_id, first_name, last_name)
SELECT cals_table.first_name, cals_table.last_name, cals_table.total_cals, minute_table.total_mins
FROM minute_table
JOIN cals_table ON minute_table.user_id = cals_table.user_id