【问题标题】:SQL Histogram: Greater than 10 orders group as "10+ bucket"SQL 直方图:大于 10 个订单组为“10+ 桶”
【发布时间】:2019-03-23 03:35:39
【问题描述】:

问题是:创建一个 sql 查询以提供 x 人发出 y 次订单的数量的直方图。任何超过 10 个订单的人都应该被分组到一个“10+”桶中

第 1 步:我创建了一个“诱惑”,如下所示:

Customerid    Order_Count
----------    -----------
CENTC         1
GROSR         2
LAZYK         2
LAUGB         3
NORTS         3
FRANR         3

第 2 步:我尝试使用 floor 函数创建直方图存储桶,但无法使用以下语法到达存储桶 '10+'

select bucket, count(*) from
(select floor(order_count/10.00)*10 as bucket from TempTable
) t group by bucket

请提出其他合适的方法。谢谢!

【问题讨论】:

    标签: sql sql-server histogram


    【解决方案1】:

    您可以使用case 表达式来定义存储桶,然后进行聚合以获得最终摘要:

    select (case when order_count >= 10 then '10+' else CustomerId
            end) as CustomerId,
           sum(Order_Count) as Order_Count
    from temptable
    group by (case when order_count >= 10 then '10+' else CustomerId
              end);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 2014-12-20
      • 1970-01-01
      • 2022-11-10
      • 2019-03-18
      • 2012-09-07
      相关资源
      最近更新 更多