如何对已计数的logu条目设置条件限制?

z9smfwbn  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(394)

所以我在我的android房间dao中有一个查询,其中我收到了训练完成百分比,计算如下:
(完成套数/总训练套数)*100
但是,我的问题是,总的训练集是通过对特定训练的每次训练的总目标集求和来计算的。
通过对每个日志条目的计数求和来计算完成的集合。因此,锻炼百分率有可能达到100%。
此外,用户可以为特定练习输入比目标集多得多的集合。我需要能够设置一个日志条目的数量是每个练习计数限制。
(日志条目表有一个junctionid,对于每个练习都是唯一的。)
如何将此查询更改为只计算每个练习的前3个logid(忽略任何超过此值的项)?
er图
以下是我目前的查询:

@Query("WITH entries AS ( SELECT COUNT(log_id) AS log_entry_count FROM log_entries_table AS l 
LEFT JOIN exercise_workout_junction_table AS e ON e.exercise_workout_id = l.junction_id 
WHERE l.date IN (:dates) AND e.workout_id = :workoutID), 
sets AS ( SELECT sum(set_number) AS set_sum FROM exercise_workout_goals_table AS eg 
LEFT JOIN exercise_workout_junction_table ej ON ej.exercise_workout_id = eg.junction_id 
WHERE ej.workout_id = :workoutID) SELECT ((SELECT log_entry_count FROM entries) *100/ 
(SELECT set_sum FROM sets))  AS workout_completion_pct" )

LiveData<String> getWorkoutCompletionPercentage(int workoutID, List<String> dates);
yduiuuwa

yduiuuwa1#

我不确定我是否正确理解了您想要的内容,但是您可以尝试这样的查询(也许您会得到主要思想并将其固定到您的用例中):

// FIRST PART - MOST CHANGES HERE
// AS a result you should get form there all exercises ID and logs (up to 3)
WITH 
entries AS 
(SELECT l.junction_id, 
    CASE  
     WHEN count(log_id) > 3 THEN 3
     ELSE count(log_id)
    END log_entry_count FROM log_entries_table AS l 
INNER JOIN exercise_workout_junction_table AS e ON e.exercise_workout_id = l.junction_id 
WHERE l.date IN (:dates) AND e.workout_id = :workoutID
GROUP BY junction_id)

// SECOND PART - IS THE SAME
sets AS ( SELECT sum(set_number) AS set_sum FROM exercise_workout_goals_table AS eg 
LEFT JOIN exercise_workout_junction_table ej ON ej.exercise_workout_id = eg.junction_id WHERE ej.workout_id = :workoutID)

// THIRD PART - LITTLE CHANGES
// Change here - sum up all the counts of entries (they already cut to 3 maximal)
SELECT ((SELECT SUM(log_entry_count) FROM entries) * 100/ 
(SELECT set_sum FROM sets)) AS workout_completion_pct

相关问题