sql server 重復與去重保留一個


select * into #water from dbo.table1 where clo_name='理化'

--創建臨時表,並保存數據

select * from ClientMatch where method in

(select method from ClientMath group by method

having COUNT(*)>1)

order by method;

--查詢出method相同的數據

 

select * from ClientMath where method in

(select method from ClientMatch group by method

having COUNT(*)>1)

and

testcode in (select testcode from ClientMath group by testconde

having COUNT(*)>1)

--查詢method和testcode都重復的數據

 

delete from ClientMacth where

method in (select method from

ClientMatch group by method

having COUNT(*)>1)

and

testcode in (select testcode

from ClientMatch group by testcode

having COUNT(*)>1)

and

ClientMatch.ID not in (select min(ClientMatch.ID) from

ClientMatch group by testcode, method

having COUNT(*)>1)

----刪除method和testcode都重復列。可以添加一個ID自增長字段

 

insert into test_methods

(methodcategory,description,method,methodrptname,iscma)

select methodcategory,description,method,methodrptname,iscma from

testcode  where testcode.method not in (select method from test_methods)

----講一個表中的部分數據插入到另一個表中,method為主鍵

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM