仅分配与条件相关的条目

发布于 2025-01-24 14:24:17 字数 1395 浏览 0 评论 0原文

我有以下代码,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云朵有点甜 2025-01-31 14:24:17

从您的逻辑中,您可以尝试使用子查询获取count然后比较

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 
        COUNT(*) filter (where D_Score<score) over (partition by student) = 0 OR score < D_Score 
        THEN 'Pass'
        ELSE 'Review' END
FROM (
    SELECT *,
       min(score) filter (where category = 'D') over (partition by student) D_Score
    FROM my_table
) t1

sqlfiddle

From your logic, you can try to use subquery to get count then compare

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 
        COUNT(*) filter (where D_Score<score) over (partition by student) = 0 OR score < D_Score 
        THEN 'Pass'
        ELSE 'Review' END
FROM (
    SELECT *,
       min(score) filter (where category = 'D') over (partition by student) D_Score
    FROM my_table
) t1

sqlfiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文