整行语句:
select * from dbo.FreightRate where enabled=0 and createddate < (select max(createddate) as mx from (select top 100 createddate from dbo.FreightRate where order by createddate)a)
分解说明:
-- 按时间取前100条数据
1.select top 100 createddate from dbo.FreightRate where order by createddate
--取当前100条数据中最大的时间
2.select max(createddate) as mx from (select top 100 createddate from dbo.FreightRate where order by createddate)a
--再小于等于最大时间,注意一定要小于等于,才包括这个时间的全部数据
3.Delete from dbo.FreightRate where createddate <= (select max(createddate) as mx from (select top 100 createddate from dbo.FreightRate where order by createddate)a)
缺点:此方法是通知读取一定范围内的最大时间来确定数据集大小的。因此不一定总是前面子查询中的TOP100,实际会大于此数据集。本查询不适用于大量相同值的createddate表,否则就此方法定量也无从谈起。