清除冗余记录的SQL语句

发表于:2007-06-08来源:作者:点击数: 标签:
问题: MI LY: 宋体; mso-bidi-font-size: 12.0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-lan

问题:MILY: 宋体; mso-bidi-font-size: 12.0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">设表T中有五个字段ABCDID,其中ID字段是为自动增量整型字段(唯一值)。业务数据生成时有重复插入现象,现需对ABCD四字段完全一样的脏数据进行清理,要求设计一SQL语句,对四个字段完全一样的记录,仅保留ID最小的一条,其余作删除处理。

DELETE FROM T WHERE [ID] IN
(
 select [ID] from T where A in
  (select A  from T Group By A,B,C,D Having count(*)>1)
 and [ID]<>
  (select min(ID) as minID  from T Group By A,B,C,D Having count(*)>1)
)

更正:
delete from T
where T.ID not in (select min(ID) from T group by T.A,T.B,T.C,T.D)

原文转自:http://www.ltesting.net