保留前n条数据,该怎么解决

保留前n条数据表结构如下:IDClassID1132143201420152016201720182019201.............265201现在要求:每个

保留前n条数据
表结构如下:
IDClassID
113
214
3201
4201
5201
6201
7201
8201
9201
.............
265201

现在要求:每个classid只保留前50条数据,有些classid没有50条记录,
请问这个sql语句该怎么写,要求一条sql语句


[解决办法]

SQL code
delete from tb where id not in (select top 50 id from tb as t where tb.ClassId=t.ClassId order by id)