求一SQL语句

cosio 2006-08-28 09:32:01
/*

1 2006-08-20 18:00:01.000
2 2006-08-20 18:00:01.000
3 2006-08-20 18:00:01.000
4 2006-08-20 18:00:01.000
1 2006-08-20 18:01:01.000
2 2006-08-20 18:01:01.000
3 2006-08-20 18:01:01.000
4 2006-08-20 18:01:01.000
1 2006-08-20 13:00:01.000

result:
--同一个IID,相差一分钟删除!
1 2006-08-20 18:00:01.000
2 2006-08-20 18:00:01.000
3 2006-08-20 18:00:01.000
4 2006-08-20 18:00:01.000
1 2006-08-20 13:00:01.000

*/
...全文
305 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
cosio 2006-08-28
  • 打赏
  • 举报
回复
OK 通过,谢谢!给分!
cosio 2006-08-28
  • 打赏
  • 举报
回复
iid iTime
----------- ------------------------------------------------------
1 2006-08-08 08:03:00.000
2 2006-08-08 08:03:00.000
3 2006-08-08 08:03:00.000

(所影响的行数为 3 行)

Yang_ 2006-08-28
  • 打赏
  • 举报
回复
delete a
from tablename a
where exists (
select 1 from tablename
where iid=a.iid
and 时间<a.时间
and 时间>=dateadd(mm,1,a.时间)
)

--写错了函数,呵呵,应该出错的呀

zsforever 2006-08-28
  • 打赏
  • 举报
回复
create table tableName(id int,is_time datetime)
insert tableName select
1, '2006-08-20 18:00:01.000' union all select
2, '2006-08-20 18:00:01.000' union all select
3, '2006-08-20 18:00:01.000' union all select
4, '2006-08-20 18:00:01.000' union all select
1, '2006-08-20 18:01:01.000' union all select
2, '2006-08-20 18:01:01.000' union all select
3, '2006-08-20 18:01:01.000' union all select
4, '2006-08-20 18:01:01.000' union all select
1, '2006-08-20 13:00:01.000' union all select
1, '2006-08-20 18:02:01.000' union all select
1, '2006-08-20 18:03:01.000'
go

delete a
from tablename a
where exists (
select 1 from tablename
where id=a.id
and is_time < a.is_time
and datediff(minute,is_time,a.is_time)<=1
)

select * from tablename
--结果
id is_time
----------- ------------------------------------------------------
1 2006-08-20 18:00:01.000
2 2006-08-20 18:00:01.000
3 2006-08-20 18:00:01.000
4 2006-08-20 18:00:01.000
1 2006-08-20 13:00:01.000

(所影响的行数为 5 行)
zsforever 2006-08-28
  • 打赏
  • 举报
回复
这是我运行刚才你给的数据
iid 时间
----------- ------------------------------------------------------
1 2006-08-08 08:02:00.000
2 2006-08-08 08:02:00.000
3 2006-08-08 08:02:00.000
4 2006-08-08 12:02:00.000

(所影响的行数为 4 行)
cosio 2006-08-28
  • 打赏
  • 举报
回复
to zsforever(虎虎) :
你去运行一下了!不合要求!会少了一条记录!
————————————————————————
给你测试的数据:
/*
测试文档

*/


/*
create table T
(
iid int,
iTime datetime
)

insert into t
select 1,'2006-08-08 08:02:00' union all
select 2,'2006-08-08 08:02:00' union all
select 3,'2006-08-08 08:02:00' union all
select 1,'2006-08-08 08:03:00' union all
select 2,'2006-08-08 08:03:00' union all
select 3,'2006-08-08 08:03:00' union all
select 4,'2006-08-08 12:02:00'
*/

zsforever 2006-08-28
  • 打赏
  • 举报
回复
delete a
from tablename a
where exists (
select 1 from tablename
where idi=a.iid
and 时间 < a.时间
and datediff(minute,时间,a.时间)<=1
)
这个好用,好象上面的缩写有问题
cosio 2006-08-28
  • 打赏
  • 举报
回复
to Zjcxc(邹建)
你那种时间就保持第一条!: 1 2006-08-20 18:00:01.000
cosio 2006-08-28
  • 打赏
  • 举报
回复
Yang_(扬帆破浪)
你的写法,得出的结果:
1 2006-08-08 08:03:00.000
2 2006-08-08 08:03:00.000
3 2006-08-08 08:03:00.000

iid=1在13:01那条没有!
不合要求!
zjcxc 2006-08-28
  • 打赏
  • 举报
回复
1 2006-08-20 18:00:01.000
1 2006-08-20 18:01:01.000
1 2006-08-20 18:02:01.000
1 2006-08-20 18:03:01.000

如果是这样的数据, 应该保留那些呢?
WangZWang 2006-08-28
  • 打赏
  • 举报
回复
delete a from tb as a
where exists(Select * from tb
where IID=a.IID and Datediff(minute,时间,a.时间)=1)
cosio 2006-08-28
  • 打赏
  • 举报
回复
保持多小了,如果有多个,只保持最小的那个!
Yang_ 2006-08-28
  • 打赏
  • 举报
回复
--没有测试
--先查询看看要删除的数据是否正确再删除
--查询语句:


select a.*
from tablename a
where exists (
select 1 from tablename
where iid=a.iid
and 时间<a.时间
and 时间>=datediff(mm,1,a.时间)
)


turenjie 2006-08-28
  • 打赏
  • 举报
回复
同一个IID,相差一分钟删除!
是删除最小时间的那个吗,如果有多个连接相邻的时间,是不是都要删除,只剩下最后的一个时间?
Yang_ 2006-08-28
  • 打赏
  • 举报
回复
delete a
from tablename a
where exists (
select 1 from tablename
where iid=a.iid
and 时间<a.时间
and 时间>=datediff(mm,1,a.时间)
)

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧