sql - 按相似数量分组 SQL (oracle sql)

标签 sql oracle

我想从下表中查找具有相似购买值(value)的销售数量:

sale_number | value 
------------+-------
 1          |    10
 2          |    11
 3          |    21 
 4          |    30 

普通的group by语句按精确的进行分组:

select count(sale_number), value from table group by value 

会给出:

count(sale_number) | value 
       ------------+-------
        1          |    10
        1          |    11
        1          |    21 
        1          |    30 

是否可以按不精确的数字和阈值(例如 +/- 10%)进行分组?给出期望的结果:

count(sale_number) | value 
       ------------+-------
        2          |    10
        2          |    11
        1          |    21 
        1          |    30 

最佳答案

您可以使用相关子查询执行您想要的操作:

select t.*,
       (select count(*)
        from t t2
        where t2.value >= t.value * 0.9 and
              t2.value <= t.value * 1.1
       ) as cnt
from t;

关于sql - 按相似数量分组 SQL (oracle sql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58218284/

相关文章:

sql - Sequelize Promises - Chaining Promises 和 Bluebird Promise.each、Promise.map、Promise.all 等

sql - 如何计算Sql Developer中两个不同列的月差?

oracle - 自动从数据库中选择日期作为 Zend_Date 对象

sqlplus - 定期将结果写入文件

php - 迪尔德 : lazy symbol binding failed for php oic8 on Apple M1

Sql(在 Oracle 上)按天计的老化报告

sql - 使用编译错误创建的触发器

c# - 开发人员开始从SQL迁移到NO-SQL(CouchDB,FathomDB,MongoDB等)时必须采取哪些 “mental steps”?

sql - 如何通过 BigQuery 中最近的时间戳加入?

mysql - 从 2 个表中选择的最简单方法,第二个是 where 条件