当前位置: 代码迷 >> Sql Server >> 剔除表中重复的数据,保留时间最早的数据,求大神指导!
  详细解决方案

剔除表中重复的数据,保留时间最早的数据,求大神指导!

热度:68   发布时间:2016-04-27 11:36:15.0
删除表中重复的数据,保留时间最早的数据,求大神指导!!
表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 allselect 13790269290,'2010-12-21 12:31:14.793' union allselect 13069879190,'2010-12-30 09:20:12.457' union allselect 13069879190,'2010-12-30 09:20:16.207' union allselect 13713014285,'2010-12-31 09:37:14.987' union allselect 13713014285,'2010-12-31 09:37:18.220' union allselect 13532892080,'2011-01-02 23:03:54.537' union allselect 13532892080,'2011-01-02 23:03:56.553'godelete  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.88713069879190    2010-12-30 09:20:12.45713713014285    2010-12-31 09:37:14.98713532892080    2011-01-02 23:03:54.537*/
  相关解决方案