| gid | gcname | gcid |
| 1 | 10元话费 | 2 |
| 2 | 10元话费 | 2 |
| 3 | 10元话费 | 2 |
| 4 | 10元话费 | 2 |
| 5 | 10元话费 | 2 |
| 6 | 10元话费 | 2 |
| 7 | 10元话费 | 2 |
| 8 | 20元话费 | 3 |
| 9 | 20元话费 | 3 |
| 10 | 20元话费 | 3 |
| 11 | 20元话费 | 3 |
| 12 | 20元话费 | 3 |
| 13 | 20元话费 | 3 |
| 14 | 20元话费 | 3 |
| 15 | 50元话费 | 4 |
| 16 | 50元话费 | 4 |
| 17 | 50元话费 | 4 |
| 18 | 50元话费 | 4 |
| 19 | 50元话费 | 4 |
分组排序的问题啊:
select * from(
select gcid,name, row_number() over (partition by gcid
order by name desc) as rowno
from tel) A
where a.rowno<=3
这个兼容性很差,而且性能不好,只有新的数据库支持。最好的办法是写存储过程,用临时表。
select * from table a where 2>(select count(id) from table b where a.gid<b.gid and a.gcid=b.gcid)
这个效率也不高,不过也是一种办法。
select * from (select gid,gcname,gcid,case when row_number() over(partition by gcid order by gcid asc) = 1 then 1
when row_number() over(partition by gcid order by gcid asc) = 2 then 2
when row_number() over(partition by gcid order by gcid asc) = 3 then 3
else 4 end as rnt
from tableA) as t
where t.rnt <= 3