表table,以下是表里的数据
member_id user_id
0001 11
0001 12
0001 13
0001 14
0002 21
0002 22
0002 23
0002 11
0004 41
0004 42
0004 43
0004 44
0004 22
0005 51
我现在想要查询出每个member_id只显示前两行数据
查询后的结果
member_id user_id
0001 11
0001 12
0002 21
0002 22
0004 41
0004 42
0005 51
select * from (select t.*,rank() over(partition by t.member_id order by t.user_id)tmpe from table t) where tmpe >2
我想把查出来的数据给删掉,怎么写,,,,,,,,,就是每个member_id下会有很多个user_id,,但是我只要每个member_id下只留2个user_id
------解决方案--------------------
把test_yixl 换成你自己的表名即可:
- SQL code
delete from test_yixl mainwhere (main.member_id, main.user_id) in (select sub.member_id, sub.user_id from (select t.member_id member_id , t.user_id user_id, rank() over(partition by t.member_id order by t.user_id) temp from test_yixl t ) sub where sub.temp >2)
------解决方案--------------------
- SQL code
delete from table where rowid in(select rowid from(select rowid,row_number() over(partition by member_id order by user_id) rn from table where rn>2))