问题:
新手来看:如何写分组取前N条的 SQL 语句?
有这样一个表:
ID RID 001 001 002 001 003 001 004 004 005 004 006 004 007 007 008 007 009 009 010 010
希望取得每组RID相同的前2条记录,即: ID RID 001 001 002 001 004 004 005 004 007 007 008 007 009 009 010 010
能否用尽可能简单的语句实现?请各位指点
回答:
select * from t as a where id in(select top 2 id from t where rid=a.rid order by id)