保留前n条数据
表结构如下:
IDClassID
113
214
3201
4201
5201
6201
7201
8201
9201
.............
265201
现在要求:每个classid只保留前50条数据,有些classid没有50条记录,
请问这个sql语句该怎么写,要求一条sql语句
[解决办法]
delete from tb where id not in (select top 50 id from tb as t where tb.ClassId=t.ClassId order by id)