簡體   English   中英

從一列獲取MAX值,從另一列獲取MIN

[英]Get MAX value from one column and MIN from another column

我一直在制作一款基於Candy Crush的游戲。 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

我需要獲得由stage_level_id分組的最高分數。 如果stage_level_id具有相同的Value (以53結尾的Value ),則它必須返回具有最小Moves數的行。

我正在嘗試以下但它沒有按預期工作:

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

預期的結果是:

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

我做錯了什么?

你的嘗試並沒有那么遙遠。 您錯過了第一個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

DEMO

您可以使用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;

not exists部分將結果限制為僅在每個組中具有最大分數的行(換句話說,不存在組內具有較高分數的其他行)。

此查詢可以利用復合索引(stage_id,value)

http://sqlfiddle.com/#!2/88ee6/8

事實上,這可以通過一個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;

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) |

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) |

這樣我們就可以在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) |

要么

| 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   |

http://sqlfiddle.com/#!9/50ed8/6/0上測試一下。

不確定我是否誤解了這個問題,但不是這么簡單:

SELECT column1, max(column2), IF(column1 like "%53", min(column3), max(column3))
FROM test
GROUP BY column1
;

看看這個, SQLFiddle鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM