想象一下虚拟数据
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中获得此信息?
发布于 2022-04-25 16:33:36
您需要具有窗口函数的条件聚合:
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,则结果将是“Pass”。如果你想要的不一样,那么你必须调整摄像机。
上面的查询为每个人在其所有行中显示一个状态。如果您希望每一行的状态不同,只需与该行的得分进行比较:
select
id, name, category, score,
case
when category = 'D' then null
when score > min(score) filter (where category = 'D') over (partition by name) then 'Review'
else 'Pass'
end as result
from mytable
order by name, id;https://stackoverflow.com/questions/72002699
复制相似问题