想象一下虛擬資料
id name category score
1 Alex A 11
2 Alex D 4
3 Bill A 81
4 Bill B 34
5 Bill D 2
6 Carl C 5
7 Carl D 10
我想應用該操作:
if score of A, B, or C > score of D
then 'Review'
else 'Pass'
所以輸出是:
id name category score conclusion
1 Alex A 11 Review
2 Alex D 4 Review
3 Bill A 81 Review
4 Bill B 34 Review
5 Bill D 2 Review
6 Carl C 5 Pass
7 Carl D 10 Pass
我怎樣才能在 PostgreSQL 中獲得這個?
uj5u.com熱心網友回復:
您想要使用視窗函式進行條件聚合:
select
id, name, category, score,
case when
max(score) filter (where category in ('A', 'B', 'C')) over (partition by name) >
min(score) filter (where category = 'D') over (partition by name)
then 'Review'
else 'Pass'
end as result
from mytable
order by name, id;
如果名稱沒有 A、B 或 C 或名稱沒有 D,則結果將為“通過”。如果你想要這個不同,那么你將不得不調整camparison。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/463920.html
標籤:sql PostgreSQL 条件语句 案子
上一篇:代號一影像未加載
