62,046
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE [dbo].[Tests](
[uID] [int] NULL,
[CreateTime] [datetime] NULL,
[Type] [int] NULL,
[TTT] [nvarchar](50) NULL
) ON [PRIMARY]
insert into [test].[dbo].[Tests] values(1,'2014-10-22 9:31:27',1,'a')
insert into [test].[dbo].[Tests] values(1,'2014-10-22 9:41:27',1,'b')
insert into [test].[dbo].[Tests] values(1,'2014-10-22 9:51:27',1,'c')
insert into [test].[dbo].[Tests] values(1,'2014-10-22 17:31:27',2,'d')
insert into [test].[dbo].[Tests] values(1,'2014-10-22 17:41:27',2,'e')
insert into [test].[dbo].[Tests] values(1,'2014-10-23 9:31:27',1,'f')
insert into [test].[dbo].[Tests] values(2,'2014-10-22 9:41:27',1,'g')
insert into [test].[dbo].[Tests] values(2,'2014-10-22 9:51:27',1,'h')
insert into [test].[dbo].[Tests] values(2,'2014-10-22 17:31:27',2,'i')
insert into [test].[dbo].[Tests] values(2,'2014-10-22 17:41:27',2,'j')
delete from tests
from tests t,
(select uid, min(createtime) createtime from Tests where TYPE = 1 group by uid, LEFT(Createtime,10)) a,
(select uid, max(createtime) createtime from Tests where TYPE = 2 group by uid, LEFT(Createtime,10)) b
where t.uid = a.uid
and t.uid = b.uid
and LEFT(t.Createtime,10) = LEFT(a.Createtime,10)
and LEFT(t.Createtime,10) = LEFT(b.Createtime,10)
and t.createtime <> a.createtime
and t.createtime <> b.createtime
;
delete from tests where exists(select 1 from tests t
inner join (
select uid, min(createtime) createtime from Tests where TYPE = 1 group by uid, LEFT(Createtime,10)) a on t.uid = a.uid
inner join (
select uid, max(createtime) createtime from Tests where TYPE = 2 group by uid, LEFT(Createtime,10)) b on t.uid = b.uid
where LEFT(t.Createtime,10) = LEFT(a.Createtime,10)
and LEFT(t.Createtime,10) = LEFT(b.Createtime,10)
and t.createtime <> a.createtime
and t.createtime <> b.createtime)
delete from tests where exists(select 1 from
(select uid, min(createtime) createtime from Tests where TYPE = 1 group by uid, LEFT(Createtime,10)) a,
(select uid, max(createtime) createtime from Tests where TYPE = 2 group by uid, LEFT(Createtime,10)) b
where t.uid = a.uid
and t.uid = b.uid
and LEFT(t.Createtime,10) = LEFT(a.Createtime,10)
and LEFT(t.Createtime,10) = LEFT(b.Createtime,10)
and t.createtime <> a.createtime
and t.createtime <> b.createtime)
delete from tests where exists(select 1 from tests t
(select uid, min(createtime) createtime from Tests where TYPE = 1 group by uid, LEFT(Createtime,10)) a,
(select uid, max(createtime) createtime from Tests where TYPE = 2 group by uid, LEFT(Createtime,10)) b
where t.uid = a.uid
and t.uid = b.uid
and LEFT(t.Createtime,10) = LEFT(a.Createtime,10)
and LEFT(t.Createtime,10) = LEFT(b.Createtime,10)
and t.createtime <> a.createtime
and t.createtime <> b.createtime)
DELETE AAA FROM
(select * ,row_number() over (partition by [uID],type,CONVERT(varchar(100), [CreateTime], 102) order by [CreateTime]) as numrow
from [test].[dbo].[Tests]) AAA where numrow>1 and AAA.Type=1
DELETE AAA FROM
(select * ,row_number() over (partition by [uID],type,CONVERT(varchar(100), [CreateTime], 102) order by [CreateTime] desc) as numrow
from [test].[dbo].[Tests]) AAA where numrow>1 and AAA.Type=2
这种sql也可以达到效果,但是Access不知道如果修改,Access大牛麻烦看看