当前位置: 代码迷 >> Sql Server >> 删除表中相同的数据,留上其最后保存的那条数据的sql语句如何写
  详细解决方案

删除表中相同的数据,留上其最后保存的那条数据的sql语句如何写

热度:34   发布时间:2016-04-27 11:06:02.0
删除表中相同的数据,留下其最后保存的那条数据的sql语句怎么写啊

我有一张表table,里面有两条相同的数据swID:762与766,删除swID较小的那一条数据,
判断两条数据相同的条件是,swCH和swTimes相同

得到的结果是


------解决方案--------------------
delete from table where SWID in(
select SWID from
(
select SWCH,swTimes,max(SWID) as SWID from table group by SWCH,swTimes
) a

)
------解决方案--------------------
[code=SQL][/code]
delete a from table a where exists(select * from table where swch=a.swch and swtimes=a.swtimes and swid>a.swid)
  相关解决方案