sqlserver清除完全重復的數(shù)據(jù)只保留重復數(shù)據(jù)中的第一條
更新時間:2014年07月23日 16:43:41 投稿:whsnow
根據(jù)autoID刪除臨時表#tmp中的重復數(shù)據(jù),只保留每組重復數(shù)據(jù)中的第一條
--創(chuàng)建測試表 CREATE TABLE [dbo].[testtab]( [id] [nchar](10) NULL, [name] [nchar](10) NULL ) ; --向測試表插入測試數(shù)據(jù) insert into testtab values('1','1'); insert into testtab values('1','1'); insert into testtab values('2','2'); insert into testtab values('2','2'); insert into testtab values('3','3'); insert into testtab values('3','3'); --創(chuàng)建臨時表并向臨時表中插入測試表testtab中數(shù)據(jù)以及添加自增id:autoID select identity(int,1,1) as autoID, * into #Tmp from testtab --根據(jù)autoID刪除臨時表#tmp中的重復數(shù)據(jù),只保留每組重復數(shù)據(jù)中的第一條 delete #Tmp where autoID in(select max(autoID) from #Tmp group by id); --清除testtab表中的所有數(shù)據(jù) delete testtab; --向testtab表中插入#Tmp表中被處理過的數(shù)據(jù) insert into testtab select id,name from #Tmp; --刪除臨時表#Tmp drop table #Tmp;
相關(guān)文章
關(guān)于數(shù)據(jù)庫優(yōu)化問題收集匯總
筆者在工作實踐中發(fā)現(xiàn),不良的SQL往往來自于不恰當?shù)乃饕O(shè)計、不充份的連接條件和不可優(yōu)化的where子句。以下就對數(shù)據(jù)庫優(yōu)化問題進行了介紹,需要的朋友可以參考下2013-07-07SQL Server附加數(shù)據(jù)庫報錯無法打開物理文件,操作系統(tǒng)錯誤5的圖文解決教程
sqlserver附加數(shù)據(jù)時,提示無法打開物理文件,操作系統(tǒng)錯誤5什么原因呢?今天小編給大家分享SQL Server附加數(shù)據(jù)庫報錯無法打開物理文件,操作系統(tǒng)錯誤5的圖文解決教程,一起看看吧2016-12-12詳解在SQLPlus中實現(xiàn)上下鍵翻查歷史命令的功能
這篇文章主要介紹了在SQLPlus中實現(xiàn)上下鍵翻查歷史命令的功能,這里介紹使用readline和rlwrap實現(xiàn)這個功能的方法,需要的朋友可以參考下2022-03-03SQL Server 2016里的sys.dm_exec_input_buffer的問題
這篇文章主要介紹了SQL Server 2016里的sys.dm_exec_input_buffer的相關(guān)資料,需要的朋友可以參考下2016-04-04將Reporting services的RDL文件拷貝到另外一臺機器時報Data at the root level i
在本機開發(fā)了一個Reporting后拷貝到服務(wù)器,然后在Sql Server Business Intelligence Development Studio中添加再打開后會報Data at the root level is invalid.錯誤2012-06-06SQL處理多級分類,查詢結(jié)果呈樹形結(jié)構(gòu)
對于多級分類常規(guī)的處理方法,很多程序員可能是用程序先讀取一級分類記錄,然后通過一級分類循環(huán)讀取下面的子分類2012-08-08