成长之路 2020-06-21
语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)
1.row_number() over()排序功能
分组排序:
已有表
E E E 9 C E A 5 B B E 8 D D C 6 E A B 6 C B D 10 C E C 4 E E D 1 D C C 8 D D E 3 B D A 9 A A C 4 C B B 3 D C A 2 C E D 10 A C C 3 D D C 1 A C D 5 E A D 1 B C A 5 C E B 8 B E B 3
执行
select f1 as user_id,count(1) as times from test_data group by f1
A 16
B 10
C 10
D 12
E 12
现要求每个次数仅展示一名用户
select user_id,times from( select user_id,times,row_number() over(partition by times order by user_id) rn from( select f1 as user_id,count(1) as times from test_data group by f1) t1) t2 where rn<2 order by times;
B 10
D 12
A 16
来源:百度站长