SQL Server 操作数据库数据的问题

原本从2017-11-28号到今天12-11号 这段时间内User表中每小时有100条数据 现在想删除数据但是每
小时要保留一组 相当于一天24条数据 求sql语句怎么写?
时间字段是dactime

参考一下:

分组后排序,赋值rownum,然后取rownum=1,就是要保留的一组, 最后删除除此之外的所有数据。

 delete * from MyTable b where b.ID not in
(
    select  t.ID  from 
    (
        select ID, row_number() over(partition by MyTime order by UserName desc) rownum from MyTable
    ) t  where t.rownum = 1 
)

delete user u where u.dactime>='2017-11-28' and u.dactime<'2017-12-11' and u.id not in (select min(id) from user u2 groupby DATEPART(yyyy,u2.dactime),DATEPART(mm,u2.dactime),DATEPART(dd,u2.dactime),DATEPART(hh,u2.dactime))

没有一个可以用的,全部有问题

分组一下再进行处理。

with tabs as(
select *,
row_number() over(
partition by substring(CONVERT(varchar(100), [DacTime],120),1,13)
order by [DacTime] desc) as num_id
from [CHCMASV21].[dbo].[INMV_DeviceDataHist]
)
delete from tabs where num_id != 1;
这也是一种解决办法

 SELECT * FROM (SELECT *,DATE(stime) AS d,SUBSTRING(stime,12,2) as h FROM tb ORDER BY 字段,stime DESC where stime<=**** and $stime>=****) tb2
GROUP BY 字段,sd,h