读书人

剔除表中重复的数据保留时间最早的数

发布时间: 2012-09-05 15:19:34 作者: rapoo

删除表中重复的数据,保留时间最早的数据,求大神指导!!
表T
A字段 B字段
137902692902010-12-21 12:31:09.887
137902692902010-12-21 12:31:14.793
130698791902010-12-30 09:20:12.457
130698791902010-12-30 09:20:16.207
137130142852010-12-31 09:37:14.987
137130142852010-12-31 09:37:18.220
135328920802011-01-02 23:03:54.537
135328920802011-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*/ 

读书人网 >SQL Server

热点推荐