简体   繁体   English

Android SQLite 获得相对于其他列的最大值

[英]Android SQLite get maximum value with respect to other columns

I have a table called 'tracked_exercises' which contains columns shown in the attached picture (exercise_name, band, reps, kgs)我有一个名为“tracked_exercises”的表,其中包含附加图片中显示的列(exercise_name、band、reps、kgs)

I'd like to create a query which given an exercise_name returns all rows which have any of these conditions for that exercise_name我想创建一个查询,给定一个运动名称,返回所有具有该运动名称的这些条件的所有行

  • Max reps with each unique band每个独特乐队的最大次数
  • Max kgs at each reps with matching band(But not if max kgs are lower or equal than max kgs with higher reps. So if you have 2 reps with 6kg, and 3 reps with 6kg I do not want the 2 reps with 6kg back)每次使用匹配带的最大公斤数(但如果最大公斤数低于或等于更高次数的最大公斤数,则不会。因此,如果您有 2 次 6 公斤,3 次 6 公斤,我不希望 6 公斤后退 2 次)

So in the example image if I give 'Muscle Up' I would get back因此,在示例图像中,如果我给“Muscle Up”,我会回来

   | band   | reps | kgs |
   | red    | 10   | 0   |
   | purple | 15   | 0   |
   | No     | 5    | 0   |
   | No     | 4    | 10  |
   | No     | 2    | 15  |

I have been able to get the maximum reps for each unique band with the below query, but haven't managed to work out how to get rows with max kgs as well我已经能够使用以下查询获得每个唯一波段的最大代表,但还没有设法解决如何获得最大公斤数的行

@Query("SELECT * FROM tracked_exercises INNER JOIN (SELECT band, MAX(reps) AS Maxreps FROM tracked_exercises WHERE exercise_name =:name GROUP BY band ) topset ON tracked_exercises.band = topset.band AND tracked_exercises.reps = topset.Maxreps")
List<TrackedExercise> getPersonalRecords(String name);

Sorry if this is not clear, comment if you need more info对不起,如果这不清楚,如果您需要更多信息,请发表评论

Example tracked_exercises table示例 tracked_exercises 表

Another example:另一个例子:

| band  | reps | kgs |
| No    | 3    | 2.5 |
| No    | 3    | 5   |
| No    | 3    | 6.25|
| No    | 10   | 0   |
| No    | 9    | 0   |
| No    | 4    | 5   |
| No    | 4    | 3.75|
| red   | 11   | 0   |
| red   | 10   | 0   |
| No    | 2    | 6.25|

I'd like the result:我想要结果:

| band  | reps | kgs |
| No    | 10   | 0   |
| red   | 11   | 0   |
| No    | 3    | 6.25|
| No    | 4    | 5   |

Edit: updated question to make it more clear编辑:更新问题以使其更清楚

Use a CTE to get all the maximums for reps and kgs and then NOT EXISTS on the results:使用CTE获得所有的最大值repskgs ,然后NOT EXISTS的结果:

with cte as (
  select t.band, t.reps, t.kgs
  from ( 
    select *,
      row_number() over (partition by band order by reps desc) rnreps,
      row_number() over (partition by band, reps order by kgs desc) rnkgs
    from tracked_exercises
  ) t  
  where t.rnreps = 1 or t.rnkgs = 1
)  
select c.* from cte c
where not exists (
  select 1 from cte
  where band = c.band and reps > c.reps and kgs >= c.kgs
)

See the demo .请参阅演示
Results:结果:

| band | reps | kgs  |
| ---- | ---- | ---- |
| No   | 10   | 0    |
| No   | 4    | 5    |
| No   | 3    | 6.25 |
| red  | 11   | 0    |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM