1

您好这个查询我正在尝试在 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
4

1 回答 1

1

MYSQL 5,7 和 Mariadb 10.1 的 NTILE

**和以前的版本**

这需要一些逻辑,如果你愿意,你可以调试它

第一个是我的应用,第二个是你查询的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<>在这里摆弄

于 2021-04-30T21:47:28.420 回答