[英]Get MAX value from one column and MIN from another column
I've been building a game based on Candy Crush. 我一直在制作一款基于Candy Crush的游戏。 The Score table has the three following columns: Score表有以下三列:
stage_level_id | value | moves
------------------------------------------------------
9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 350
9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 500
9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 15000 | 125
9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 13500 | 100
9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 12500 | 350
9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 7500 | 25
I need to get the top Score grouped by stage_level_id
. 我需要获得由stage_level_id
分组的最高分数。 If an stage_level_id
have the same Value
(as the one ending with 53), it must return the row with the smallest number of Moves
. 如果stage_level_id
具有相同的Value
(以53结尾的Value
),则它必须返回具有最小Moves
数的行。
I'm trying the following but it's not working as expected: 我正在尝试以下但它没有按预期工作:
SELECT a.stage_level_id, MAX(a.value) as max_value, a.moves
FROM scores a
LEFT JOIN scores b ON (
a.stage_level_id = b.stage_level_id
)
RIGHT JOIN scores c ON (
c.moves = ( SELECT MIN(moves) as moves FROM scores WHERE c.stage_level_id = a.stage_level_id )
)
WHERE a.player_id = 1475332386040815
GROUP BY a.stage_level_id
The expected result is: 预期的结果是:
stage_level_id | value | moves
------------------------------------------------------
9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 350
9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 15000 | 125
9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 12500 | 350
What I'm doing wrong? 我做错了什么?
Your attempt wasn't that far off. 你的尝试并没有那么遥远。 You were missing a necessary part of the first JOIN ... ON clause though, and the second JOIN isn't necessary. 您错过了第一个JOIN ... ON子句的必要部分,但第二个JOIN不是必需的。
SELECT tbl1.stage_level_id, tbl1.max_value, MIN(s.moves) AS moves
FROM
(
SELECT stage_level_id, MAX(value) AS max_value
FROM scores
GROUP BY stage_level_id
) tbl1
LEFT JOIN scores s ON tbl1.stage_level_id = s.stage_level_id AND tbl1.max_value = s.value
GROUP BY stage_level_id
You can get the minimum number of moves for each (stage_id,max score) group using NOT EXISTS
您可以使用NOT EXISTS
获取每个(stage_id,max score)组的最小移动次数
select stage_level_id, max(value), min(moves)
from scores s1
where not exists (
select 1 from scores s2
where s2.stage_level_id = s1.stage_level_id
and s2.value > s1.value
)
group by stage_level_id;
The not exists
part limits the results to only those rows that have the maximum score within each group (in other words no other row with a higher score within the group exists). not exists
部分将结果限制为仅在每个组中具有最大分数的行(换句话说,不存在组内具有较高分数的其他行)。
This query can take advantage of a composite index on (stage_id,value)
此查询可以利用复合索引(stage_id,value)
http://sqlfiddle.com/#!2/88ee6/8 http://sqlfiddle.com/#!2/88ee6/8
In fact, this can be done simply by one select
with a left join
and a group by
: 事实上,这可以通过一个select
left join
和一个group by
:
select s1.stage_level_id, s1.score, min(s1.moves) as moves
from scores s1
left join scores s2
on s2.stage_level_id = s1.stage_level_id
and s2.score > s1.score
where s2.score IS NULL
group by s1.stage_level_id;
The left join
gives: left join
给出:
| s1 | s2 |
|--------------------------------------|-------|-------|--------------------------------------|--------|--------|
| stage_level_id | score | moves | stage_level_id | score | moves |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 350 | (null) | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 500 | (null) | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 15000 | 125 | (null) | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 13500 | 100 | 9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 15000 | 125 |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 7500 | 25 | 9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 12500 | 350 |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 12500 | 350 | (null) | (null) | (null) |
The where
clause selects the rows that contain the highest score(s) per stage_level_id
: where
子句选择每个stage_level_id
包含最高分数的行:
| s1 | s2 |
|--------------------------------------|-------|-------|----------------|--------|--------|
| stage_level_id | score | moves | stage_level_id | score | moves |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 350 | (null) | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 500 | (null) | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 15000 | 125 | (null) | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 12500 | 350 | (null) | (null) | (null) |
so that we can run min(s1.moves)
on each stage_level_id
group formulated by group by
to get the final result: 这样我们就可以在group by
公式化的每个stage_level_id
组上运行min(s1.moves)
来获得最终结果:
| s1 | s2 |
|--------------------------------------|-------|-------|----------------|--------|--------|
| stage_level_id | score | moves | stage_level_id | score | moves |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 350 | (null) | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 15000 | 125 | (null) | (null) | (null) |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 12500 | 350 | (null) | (null) | (null) |
or 要么
| stage_level_id | score | moves |
|--------------------------------------|-------|-------|
| 9f7678f0-fc8f-11e3-a398-b2227cce2b53 | 35000 | 350 |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b54 | 15000 | 125 |
| 9f7678f0-fc8f-11e3-a398-b2227cce2b55 | 12500 | 350 |
Test it out at http://sqlfiddle.com/#!9/50ed8/6/0 . 在http://sqlfiddle.com/#!9/50ed8/6/0上测试一下。
Not sure if I misunderstood the question but isnt it as simple as this: 不确定我是否误解了这个问题,但不是这么简单:
SELECT column1, max(column2), IF(column1 like "%53", min(column3), max(column3))
FROM test
GROUP BY column1
;
Check this out, SQLFiddle link 看看这个, SQLFiddle链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.