将金额分类的案例陈述

Case statement to bucket amounts into categories

如果我想创建一个将列值分桶到一个新列中的 case 语句,我该如何在没有多个 case 语句的情况下执行此操作?

ID | Amount
B     200
W     300
B     300
W     20

等等

输出将按 ID 分组,并且该 ID 的金额总和有一个新列将它们分入一个类别,但在一个 case 语句中?

ID | Amount | Bucket
B     600     >= 500 
W     320     >=0 and <=500 

谢谢

你可以这样做:

select id, sum(amount) as amount,
       (case when sum(amount) >= 0 and sum(amount) < = 500
             then '>= 0 and <= 500' 
             when sum(amount) > 500 
             then '> 500' 
        end) as Bucket
from table t
group by id;