[英]Hierarchical MAX
當考慮兩個字段時,有沒有一種方法可以使行具有MAX(),並賦予其中一個優先級。 例如
ID Column_A Column_B
----------------------------
1 1 3
2 3 2
3 2 2
4 3 1
會回來
ID Column_A Column_B
----------------------------
2 3 2
因為ID的2和4在Column_A上均為MAX,但#2在Column_B上“獲勝”。
我想用偽代碼執行此查詢的操作:
If (It's the MAX on A, tied with nothing)
Return it
Else If (It's tied for MAX on A and it's MAX [or tied] on B)
Return it
你可以嘗試...
SELECT *
FROM mytable
ORDER BY Column_A DESC, Column_B DESC
LIMIT 1;
(如果我正確理解了這個問題)。
根據Matthew Purdon的建議編輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.