我想写一个查询,我的PARTITION BY
和ORDER BY
依赖于相关表格中的值:
如果movements.has_weight: true
的值为:
SELECT DISTINCT FIRST_VALUE(id)
OVER
(PARTITION BY user_id, movement_id, reps ORDER BY weight DESC)
AS id
FROM logs
INNER JOIN movements
ON logs.movement_id = movements.id
如果movements.has_weight: false
:
SELECT DISTINCT FIRST_VALUE(id)
OVER
-- cf the above, we don't partition over reps, and we order by reps instead of weight
(PARTITION BY user_id, movement_id ORDER BY reps DESC)
AS id
FROM log
INNER JOIN movements
ON logs.movement_id = movements.id
查询的重点是举重日志: 对于每个移动列表,记录的最大重复次数(如果没有重量,例如上推),或者如果有重量,则列出每个重复次数以及记录的最高权重(例如卧推)。
我可以在一个查询中实现此目的吗?