成都公司:成都市成華區(qū)建設(shè)南路160號1層9號
重慶公司:重慶市江北區(qū)紅旗河溝華創(chuàng)商務(wù)大廈18樓
當(dāng)前位置:工程項目OA系統(tǒng) > 領(lǐng)域應(yīng)用 > 微信營銷 > 微信營銷軟件
SQL去除重復(fù)記錄
有兩個意義上的重復(fù)記錄,一是完全重復(fù)的記錄,也即所有字段均重復(fù)的記錄,二是部分關(guān)鍵字段重復(fù)的記錄,比如Name字段重復(fù),而其他字段不一定重復(fù)或都重復(fù)可以忽略。 1、對于第一種重復(fù),比較容易解決,使用 select distinct * from tableName 就可以得到無重復(fù)記錄的結(jié)果集。 如果該表需要刪除重復(fù)的記錄(重復(fù)記錄保留1條),可以按以下方法刪除 select distinct * into #Tmp from tableName drop table tableName select * into tableName from #Tmp drop table #Tmp 發(fā)生這種重復(fù)的原因是表設(shè)計不周產(chǎn)生的,增加唯一索引列即可解決。 2、這類重復(fù)問題通常要求保留重復(fù)記錄中的第一條記錄,操作方法如下 假設(shè)有重復(fù)的字段為Name,Address,要求得到這兩個字段唯一的結(jié)果集 select identity(int,1,1) as autoID, * into #Tmp from tableName select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,Address select * from #Tmp where autoID in(select autoID from #tmp2) 最后一個select即得到了Name,Address不重復(fù)的結(jié)果集(但多了一個autoID字段,實際寫時可以寫在select子句中省去此列) or With Dups as ( select *, row_number() over (partition by Product_Code order by Product_Code) as RowNum from #prod ) Delete from Dups where rownum > 1;
如果認(rèn)為此文對您有幫助,別忘了支持一下哦!
自己用distinct的記錄如下:
[java]view plaincopy

<!--客戶批次列表-->
<selectid="listPageCusBatch"parameterType="paramMap"resultType="pd">
selectdistincttp.post_batchNo,tp.post_time,tp.customer_id,tc.customer_name
FROMtb_dispatch_mail_posttp
LEFTJOINtb_dispatch_customertcONtp.customer_id=tc.customer_id
<where>
tp.post_batchnoisnotnull
<iftest="paramMap.postTimeStart!=nullandparamMap.postTimeStart.length()>0">
ANDTO_DATE(#{paramMap.postTimeStart},'yyyy-mm-ddhh24:mi:ss')<=tp.post_time
</if>
<iftest="paramMap.postTimeEnd!=nullandparamMap.postTimeEnd.length()>0">
ANDTO_DATE(#{paramMap.postTimeEnd},'yyyy-mm-ddhh24:mi:ss')>=tp.post_time
</if>
</where>
ORDERBYtp.post_batchnoDESC
</select>
本站推薦
- 1巧用SQL搭建高效實用的庫存管理系統(tǒng)
- 2sql庫存管理系統(tǒng)搭建方法與高效實踐技巧
- 3SQL庫存管理系統(tǒng)價格背后的驚人秘密
- 4sql做庫存管理系統(tǒng)的實用方法與高效技巧
- 5java和sql庫存信息管理系統(tǒng)助企業(yè)高效控庫存降成本
- 6巧用SQL數(shù)據(jù)庫管理系統(tǒng)打造高效物流管理解決方案
- 7C語言與SQL打造高效物流信息管理系統(tǒng)提升物流運作效率
- 8aspsql倉庫管理系統(tǒng),實現(xiàn)庫存精準(zhǔn)管控與高效出入庫管理的得力工具
- 9aspsql倉庫管理系統(tǒng)源代碼,助力倉庫高效管理的實用代碼資源

