34,593
社区成员
发帖
与我相关
我的任务
分享
delete a from tb as a where ID not exists(select max(ID) from tb)
delete a from tb a where exists (select 1 from (select row_number() over(order by getdate()) as id,* from tb) x where a.id>x.id and a.年=x.年 and a.月=x.月 and a.日=x.日 and a.时=x.时 and a.分=x.分)
select * from 表名1 a where exists (select 1 from 表名 where 表名.年=a.年 and 表名.月=a.月 and 表名.日= a.日
and 表名.时 = a.时 and 表名.分 = a.分)
use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
年 int,
月 int,
日 int,
时 int,
分 int
)
go
--插入测试数据
insert into tb select 2010,3,5,10,35
union all select 2010,3,5,10,35
union all select 2010,3,5,10,35
union all select 2010,7,15,9,24
union all select 2010,7,15,9,24
union all select 2010,9,20,6,43
go
--代码实现
select * from tb
/*测试结果
年 月 日 时 分
---------------------------------------------
2010 3 5 10 35
2010 3 5 10 35
2010 3 5 10 35
2010 7 15 9 24
2010 7 15 9 24
2010 9 20 6 43
(6 行受影响)
*/
;with t as(select idd=row_number()over(partition by 年,月,日,时,分 order by getdate()),* from tb)
delete t from t where idd!=1
select * from tb
/*测试结果
年 月 日 时 分
---------------------------------------------
2010 3 5 10 35
2010 7 15 9 24
2010 9 20 6 43
(3 行受影响)
*/
;with cte as
(
select rn = row_number()over(partition by 年,月,日,时,分 order by getdate()), * from 有一张表
)
delete from cte where rn > 1
select *
from 表名1 a
where exists (select 1 from 表名 where 表名.年=a.年 and 表名.月=a.月 and 表名.日= a.日
and 表名.时 = a.时 and 表名.分 = a.分)