我在Presto上使用窗口函数对每个分区进行分组,结果返回为extected,但我只想获取每个分区的前10条记录。但如果我应用LIMIT10,那么它在所有分区中只返回10行,而不是单个分区注意:我在这里看到过类似的问题,但看起来很复杂。有什么简单的方法可以解决这个问题吗?
使用的查询:
WITH TOP_USERS AS (
SELECT NAME, UNAME, DATE,
ROW_NUMBER() OVER (PARTITION BY NAME,UNAME ORDER BY DATE ASC) AS RN FROM TABLE_NAME
)
SELECT * FROM TOP_USERS WHERE RN=1 ORDER BY NAME LIMIT 100
当前结果:
NAME UNAME DATE RN
BBIG StockHighArmy 2021-01-07 01:07:04.000 1
BCII VinnytheLEFT 2021-01-07 09:54:35.000 1
BEGI RadioSilentplay 2021-01-07 10:38:45.000 1
BEGI aheadsupotc 2021-01-07 10:42:51.000 1
BETRF greg_lechuga 2021-01-07 09:23:55.000 1
BIEI trade_dr 2021-01-07 08:57:10.000 1
BIEI ThornLeafArtist 2021-01-07 09:20:37.000 1
BIEI Vegas_Whoa 2021-01-07 00:57:12.000 1
BIEI trader_bearded 2021-01-07 08:13:53.000 1
BITCF OTCBullRider 2021-01-07 10:29:11.000 1
BKLLF OddStockTrader 2021-01-07 10:04:45.000 1
BLDV A10_Vet 2021-01-07 10:33:19.000 1
BLPG NedGreen5 2021-01-07 09:54:07.000 1
BLPG ThebuyG 2021-01-07 09:38:16.000 1
BLPG StockRocket1 2021-01-07 09:20:20.000 1
BLPG aheadsupotc 2021-01-07 09:57:30.000 1
BLPG mando2250 2021-01-07 01:39:52.000 1
BLPG crabbermike 2021-01-07 08:28:08.000 1
BLPG Scottspg1033 2021-01-07 06:20:08.000 1
BLPG thestocks7889 2021-01-07 00:24:06.000 1
BLPG koba19822012 2021-01-07 09:28:15.000 1
BLPG StockStar319 2021-01-07 09:32:11.000 1
BLPG rrr1sr 2021-01-07 09:16:25.000 1
BLPG BodaciousTrade 2021-01-07 09:52:49.000 1
BLPG TheChaseGroup1 2021-01-07 06:23:58.000 1
BLPG rj34704726 2021-01-07 00:57:22.000 1
BLPG claydeath1 2021-01-07 09:22:35.000 1
BLPG Chivitotrader 2021-01-07 09:38:08.000 1
预期结果:(SEE BLPG只有10行(
NAME UNAME DATE RN
BBIG StockHighArmy 2021-01-07 01:07:04.000 1
BCII VinnytheLEFT 2021-01-07 09:54:35.000 1
BEGI RadioSilentplay 2021-01-07 10:38:45.000 1
BEGI aheadsupotc 2021-01-07 10:42:51.000 1
BETRF greg_lechuga 2021-01-07 09:23:55.000 1
BIEI trade_dr 2021-01-07 08:57:10.000 1
BIEI ThornLeafArtist 2021-01-07 09:20:37.000 1
BIEI Vegas_Whoa 2021-01-07 00:57:12.000 1
BIEI trader_bearded 2021-01-07 08:13:53.000 1
BITCF OTCBullRider 2021-01-07 10:29:11.000 1
BKLLF OddStockTrader 2021-01-07 10:04:45.000 1
BLDV A10_Vet 2021-01-07 10:33:19.000 1
BLPG NedGreen5 2021-01-07 09:54:07.000 1
BLPG ThebuyG 2021-01-07 09:38:16.000 1
BLPG StockRocket1 2021-01-07 09:20:20.000 1
BLPG aheadsupotc 2021-01-07 09:57:30.000 1
BLPG mando2250 2021-01-07 01:39:52.000 1
BLPG crabbermike 2021-01-07 08:28:08.000 1
BLPG Scottspg1033 2021-01-07 06:20:08.000 1
BLPG thestocks7889 2021-01-07 00:24:06.000 1
BLPG koba19822012 2021-01-07 09:28:15.000 1
BLPG StockStar319 2021-01-07 09:32:11.000 1
BLPG rrr1sr 2021-01-07 09:16:25.000 1
BLPG BodaciousTrade 2021-01-07 09:52:49.000 1
BLPG TheChaseGroup1 2021-01-07 06:23:58.000 1
您需要更改where
子句:
WHERE RN <= 10
修复PARTITION BY
和WHERE RN < 11
子句后得到预期结果,如下所示。但我仍然不知道为什么PARTITION BY NAME,UNAME不能一起正确地从1,2,3,4……方式递增生成行号?对于多个PARTITION BY列,它的行为可能类似于RANK,而不是ROW_NUMBER。如果我错了,请纠正我。
WITH TOP_USERS AS (
SELECT NAME, UNAME, DATE,
ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY DATE ASC) AS RN FROM TABLE_NAME
)
SELECT * FROM TOP_USERS WHERE RN < 11 ORDER BY NAME LIMIT 100