【问题标题】:Case statement to bucket amounts into categories将金额分类的案例语句
【发布时间】:2020-06-04 14:51:46
【问题描述】:

如果我想创建一个 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 

谢谢

【问题讨论】:

    标签: mysql sql presto


    【解决方案1】:

    你可以这样做:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 2013-09-23
      • 1970-01-01
      相关资源
      最近更新 更多