gpt4 book ai didi

postgresql - postgres "ntile(2) over (order by column)"无效订单

转载 作者:行者123 更新时间:2023-11-29 13:45:26 27 4
gpt4 key购买 nike

我注意到在大表上使用 postgres ntile 会出现奇怪的行为。它并没有真正正确排序。例子是:

select tile, min(price) as minprice, max(price) as maxprice, count(*) 
from (
select ntile(2) over(order by price ASC) as tile, adult_price as price
from statistics."trips"
where back_date IS NULL AND adult_price IS NOT null
and departure = 297 and arrival = 151
) as t
group by tile

在我看来,它给出了非常奇怪的结果:

tile    minprice    maxprice    count
1 2250 5359 74257
2 2250 27735 74257

count 和 maxprice 没有什么特别之处。对于某些数据集,它可能是正确的。但是 min_price 表明排序有问题。

第 2 个图 block 的 min_price 怎么可能小于第 1 个图 block 的 max_price?但这是通过检查内部选择的结果来批准的。它仅部分订购,并且有一些“混合”部分,价格顺序被打破。

手动分成两部分显示相同的最低价和最高价,但中间价不同:

select * from (
select row_number() over (order by adult_price ASC) as row_number, adult_price
from statistics."trips"
where back_date IS NULL AND adult_price IS NOT null
and departure = 297 and arrival = 151
order by price
) as t
where row_number IN(1, 74257, 74258, 148514)

row_number adult_price
1 2250
74257 4075
74258 4075
149413 27735

最佳答案

肯定是混淆的列名。

外层查询中的price列实际上是内层查询中的adult_price,而ntile是按不同的列计算排序的( price 在内部查询中)。

关于postgresql - postgres "ntile(2) over (order by column)"无效订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49151375/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com