仅分配与条件相关的条目
我有以下代码,
with my_table (id, student, category, score)
as (values
(1, 'Alex', 'A', 11),
(2, 'Alex', 'D', 4),
(3, 'Bill', 'A', 81),
(4, 'Bill', 'B', 1),
(5, 'Bill', 'D', 22),
(6, 'Carl', 'C', 5),
(7, 'Carl', 'D', 10)
)
select
id, student, category, score,
case when
max(score) filter (where category in ('A', 'B', 'C')) over (partition by student) >
min(score) filter (where category = 'D') over (partition by student)
then 'Review'
else 'Pass'
end as result
from my_table
order by student, id
该代码可以输出
id student category score conclusion
1 Alex A 11 Review
2 Alex D 4 Review
3 Bill A 81 Review
4 Bill B 1 Review
5 Bill D 22 Review
6 Carl C 5 Pass
7 Carl D 10 Pass
如何编辑它,因此仅将A,B或C大于D的条目分配给它们。因此,在这种情况下,所需的输出将是:
id student category score conclusion
1 Alex A 11 Review
2 Alex D 4 Review
3 Bill A 81 Review
4 Bill B 1 Pass
5 Bill D 22 Review
6 Carl C 5 Pass
7 Carl D 10 Pass
对于比尔,a> d因此将其分配给它; b< d SO分配给它。
I have the following code
with my_table (id, student, category, score)
as (values
(1, 'Alex', 'A', 11),
(2, 'Alex', 'D', 4),
(3, 'Bill', 'A', 81),
(4, 'Bill', 'B', 1),
(5, 'Bill', 'D', 22),
(6, 'Carl', 'C', 5),
(7, 'Carl', 'D', 10)
)
select
id, student, category, score,
case when
max(score) filter (where category in ('A', 'B', 'C')) over (partition by student) >
min(score) filter (where category = 'D') over (partition by student)
then 'Review'
else 'Pass'
end as result
from my_table
order by student, id
which outputs
id student category score conclusion
1 Alex A 11 Review
2 Alex D 4 Review
3 Bill A 81 Review
4 Bill B 1 Review
5 Bill D 22 Review
6 Carl C 5 Pass
7 Carl D 10 Pass
How can I edit it so only the entries where either A, B, or C are larger than D are assigned 'Review' to them. So in this case, the desired output would be:
id student category score conclusion
1 Alex A 11 Review
2 Alex D 4 Review
3 Bill A 81 Review
4 Bill B 1 Pass
5 Bill D 22 Review
6 Carl C 5 Pass
7 Carl D 10 Pass
For Bill, A>D so Review is assigned to it; B<D so Pass is assigned to it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的逻辑中,您可以尝试使用子查询获取
count
然后比较sqlfiddle
From your logic, you can try to use subquery to get
count
then comparesqlfiddle