PostgreSQL:基于两列条件的情况
虚拟数据
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中获得此操作?
Imagine the dummy data
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
I would like to apply the action:
if score of A, B, or C > score of D
then 'Review'
else 'Pass'
So the output is:
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
how can I obtain this in PostgreSQL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用窗口函数的有条件汇总:
如果名称没有A,B或C,则名称没有D,则结果将为“通过”。如果您想要的是不同的,那么您必须调整Camparison。
上述查询使您每行显示一个状态。如果您想要每行不同的状态,只需与该行的分数进行比较:
You want conditional aggregation with window functions:
If there is no A, B or C for a name or no D for a name, the result will be 'Pass'. If you want this differently, then you'll have to adjust the camparison.
The above query gets you a status per person shown in all their rows. If you want a different status per row instead, just compare with the row's score: