SQL 直方图 : Greater than 10 orders group as "10+ bucket"

标签 sql sql-server histogram

问题是:创建一个 sql 查询来提供 x 人下了 y 个订单的数量的直方图。订单数超过 10 的任何人都应归入“10+”存储桶

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

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

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

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

请建议其他合适的方法。谢谢!

最佳答案

您可以使用 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);

关于SQL 直方图 : Greater than 10 orders group as "10+ bucket",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55310385/

相关文章:

mysql - 将2行合并为一行,使用2列MYSQL

c# - 如何检查文本框中的值是否已存在于连接的数据库中

mysql - mysql创建临时表存储过程报错

Sql Server 按特定顺序显示项目

sql - 是否可以通过 bcp 将 'PRINT' 命令导出到文件中?

sql-server - 在存储过程中调用带输出参数的存储过程 - 列名无效

python - 具有非数值数据的 Matplotlib 直方图

sql - rails : Query nil has_one association

r - R 中的叠加直方图(首选 ggplot2)

image-processing - 在 SIMD 中矢量化直方图的方法?