我的表Schema是(State string,City String,Size int)
这是我的输入数据
Karnataka,Bangalore,200
Karnataka,Mysore,50
Karnataka,Bellary,100
Karnataka,Mangalore,10
Andhra pradesh,Chittoor, 25
Andhra pradesh,nellore, 15
Andhra pradesh,guntur, 20
Andhra pradesh,tirupathi, 30
Andhra pradesh,vizag, 35
Andhra pradesh,kadapa, 45
我想检索州的前两个城市以及大小,我想要输出如下。
(Andhra pradesh,{(35),(30)},{(vizag),(tirupathi)})
(Karnataka,{(200),(100)},{(Bangalore),(Bellary)})
我已经将查询写成如下,但我得到的错误是表达式不是按组大小,plz帮助我。
select * from statefile groyp by state,city order by size limit 2;
提前感谢。
答案 0 :(得分:1)
您可以使用row_number()
:
select sf.*
from (select sf.*,
row_number() over (partition by state order by size desc) as seqnum
from statefile sf
) sf
where seqnum <= 2;