创建存储桶以使用 MySQL 对值进行分类

Creating buckets to categorize the values using MySQL

您好,我正在尝试为大量行创建存储桶。我的最大值为 9759721,最小值为 1006909。我想显示如下结果:

distance bucket range
1006909 0 1000000 - 1009999
1013525 1 1010000 - 1019999
1021948 2 1020000 - 1029999

table 可能不太清楚,但总的来说,我想通过更改 10000 来分解它们。从 [=15= 开始,每隔 10000 创建一个新桶].

我尝试了以下代码,但它没有显示正确的输出。

select distance,floor(distance/10000) as _floor  from data;

我得到了类似的东西:

distance bucket
1006909 100
1013525 101
1021948 102
1035472 103
1042069 104
9759721 975

这似乎是正确的,但我需要桶从 0 开始,然后根据 10000 进行更改。然后还有一个范围列。我对距离的最小值是 1006909,因此数据不是以 0 开头,但是否仍然有一个从 0 开始的桶列 [即分配给最小距离]。

SELECT
  d.distance,
  DENSE_RANK() OVER (ORDER BY d._floor) - 1  AS bucket,
  d._floor * 10000                           AS bucket_lower_limit,
  d._floor * 10000 + 10000                   AS bucket_upper_limit
FROM
(
  SELECT
    distance,
    FLOOR(distance / 10000)  AS _floor
  FROM
    data
)
  AS d

注意: 将给出从 0 向上编号的桶,但也会删除所有间隙 (这样您的示例数据最后一行将包含存储桶 5,而不是存储桶 975)

或者,如果您需要保留间隙...

SELECT
  d.distance,
  d._floor - MIN(d._floor) OVER ()   AS bucket,
  d._floor * 10000                   AS bucket_lower_limit,
  d._floor * 10000 + 10000           AS bucket_upper_limit
FROM
(
  SELECT
    distance,
    FLOOR(distance / 10000)  AS _floor
  FROM
    data
)
  AS d

只需计算 1006909 div 10000 * 10000 = 1000000 并从距离中减去它。这将使桶从 0:

开始
SELECT distance
     , (distance - a) div 10000 AS bucket
     , distance div 10000 * 10000 AS range_from
     , distance div 10000 * 10000 + (10000 - 1) AS range_to
FROM t
CROSS JOIN (
  SELECT MIN(distance) div 10000 * 10000 AS a
  FROM t
) AS x

SQL Fiddle