日期:2014-05-17  浏览次数:20534 次

删除表中重复的数据,保留时间最早的数据,求大神指导!!
表T
A字段 B字段  
13790269290 2010-12-21 12:31:09.887
13790269290 2010-12-21 12:31:14.793
13069879190 2010-12-30 09:20:12.457
13069879190 2010-12-30 09:20:16.207
13713014285 2010-12-31 09:37:14.987
13713014285 2010-12-31 09:37:18.220
13532892080 2011-01-02 23:03:54.537
13532892080 2011-01-02 23:03:56.553

------解决方案--------------------
SQL code

----------------------------
-- Author  :TravyLee(努力工作中!!!)
-- Date    :2012-08-08 13:13:15
-- Version:
--      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) 
--    Apr  2 2010 15:48:46 
--    Copyright (c) Microsoft Corporation
--    Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7600: ) (Hypervisor)
--
----------------------------
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
go 
create table [test]([A字段] bigint,[B字段] datetime)
insert [test]
select 13790269290,'2010-12-21 12:31:09.887' union all
select 13790269290,'2010-12-21 12:31:14.793' union all
select 13069879190,'2010-12-30 09:20:12.457' union all
select 13069879190,'2010-12-30 09:20:16.207' union all
select 13713014285,'2010-12-31 09:37:14.987' union all
select 13713014285,'2010-12-31 09:37:18.220' union all
select 13532892080,'2011-01-02 23:03:54.537' union all
select 13532892080,'2011-01-02 23:03:56.553'
go
delete  from test 
where not exists(
select 1 from test b where test.A字段=b.A字段 and test.B字段<b.B字段
)

select * from test

/*

A字段    B字段
-----------------------------------------------
13790269290    2010-12-21 12:31:09.887
13069879190    2010-12-30 09:20:12.457
13713014285    2010-12-31 09:37:14.987
13532892080    2011-01-02 23:03:54.537

*/