最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SQL去除重復(fù)記錄(七種)

 更新時間:2017年02月16日 16:56:16   作者:小熊吉米  
本文主要介紹了SQL去除重復(fù)記錄的七種方法。具有很好的參考價值,下面跟著小編一起來看下吧

話不多說,請看代碼:

if not object_id('Tempdb..#T') is null
 drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 5,N'B',N'B2'
Go 

--I、Name相同ID最小的記錄(推薦用1,2,3),保留最小一條

方法1:

delete a from #T a where  exists(select 1 from #T where Name=a.Name and ID<a.ID)

方法2:

delete a from #T a left join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null

方法3:

delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)

方法4(注:ID為唯一時可用):

delete a from #T a where ID not in(select min(ID)from #T group by Name)

方法5:

delete a from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)>0

方法6:

delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID)

方法7:

delete a from #T a where ID>any(select ID from #T where Name=a.Name)
select * from #T

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

最新評論

武义县| 洛阳市| 章丘市| 博野县| 雷波县| 永定县| 遂溪县| 清原| 宿松县| 合川市| 仲巴县| 海林市| 北安市| 揭西县| 习水县| 汕尾市| 莱西市| 夏河县| 香格里拉县| 平罗县| 寻甸| 霍州市| 杂多县| 奉贤区| 潜山县| 阿瓦提县| 碌曲县| 江孜县| 安塞县| 志丹县| 溧水县| 绵竹市| 满城县| 左权县| 宁国市| 渭源县| 岳普湖县| 鸡泽县| 本溪市| 白城市| 新河县|