Mysql 中的 NTILE 函数在列中得到很好的分布

NTILE function in Mysql get to well distribution over column

您好这个查询,我正在尝试 运行 在 MySQL 中很好地分配我的 ID,但看起来语法有问题。

select min(ID), max(ID),count(*), nt from ( select ID, ntile(16) over (order by ID) nt from table) group by nt order by nt;

这在 Oracle 中有效,但在 MySQL 中无效,可能看起来在 MySQL 5.7 中不可用。 我们还能如何获得这些数据?

基本上我生成了 UUID 应用程序,可以订购,需要组织和分组,然后分成 16 段。

预期输出

MIN(ID)                                 MAX(ID)                       COUNT(*)               NT                                                           
                                                         
00000000-ebc5-4d19-9d7b                 0a360b83-6d9a-17d7-9b67            36282227          1                   
0a360b83-6d9a-17d7-9b67                 0a360b85-6ebb-1bbc-9bbb            36282227          2

NTILE for MYSQL 5,7 和 Mariadb 10.1

**和之前的版本**

它需要一点逻辑,如果你喜欢你可以调试它

第一个是我申请的,第二个是mysql你查询的80版本对比

我仍然建议升级您的 mysql 版本

主要部分是

 @mod:=countr % 16, @div:=countr DIV 16

在何处确定所需的图块数量

SELECT 
    MIN(ID), MAX(ID), COUNT(*), nt
FROM
    (SELECT 
        `ID`,
            IF(@countr < @div2, @ntile, @ntile:=@ntile + 1) AS nt,
            IF(@countr < @div2, @countr:=@countr + 1, @countr:=1) c1,
            IF(@ntile <= CAST(@mod AS UNSIGNED), @div2:=@div + 1, @div2:=@div) div2
    FROM
        (SELECT 
        ID, @mod:=countr % 16, @div:=countr DIV 16, @div2:=@div
    FROM
        table1, (SELECT 
        COUNT(*) countr
    FROM
        table1, (SELECT @ntile:=1, @countr:=0, @div2:=0) t3) t2) t1
    ORDER BY ID) t1
GROUP BY nt
ORDER BY CAST(nt AS UNSIGNED);
select 
min(ID)
, max(ID)
,count(*)
, nt 
from 
( select 
      ID
        , ntile(16) over (order by ID) nt 
  from table1)  t1
group by nt order by nt;

db<>fiddle here