監(jiān)理公司管理系統(tǒng) | 工程企業(yè)管理系統(tǒng) | OA系統(tǒng) | ERP系統(tǒng) | 造價咨詢管理系統(tǒng) | 工程設(shè)計(jì)管理系統(tǒng) | 甲方項(xiàng)目管理系統(tǒng) | 簽約案例 | 客戶案例 | 在線試用
X 關(guān)閉

SQL去除重復(fù)記錄

申請免費(fèi)試用、咨詢電話:400-8352-114

  有兩個意義上的重復(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è)計(jì)不周產(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字段,實(shí)際寫時可以寫在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>

發(fā)布:2007-04-07 16:32    編輯:泛普軟件 · xiaona    [打印此頁]    [關(guān)閉]
相關(guān)文章:

泛普微信營銷軟件其他應(yīng)用

微營銷 微信推廣 微信營銷軟件 微信二維碼 微信商城 微信支付 微信開店 微信公眾平臺 微信公眾號 微信公眾平臺登錄 微信怎么玩 微信認(rèn)證 下載微信到手機(jī) 手機(jī)微信 微信官網(wǎng) 微信網(wǎng)頁版 電腦微信 微信電腦版 微信公眾平臺注冊 微信注冊 微信公眾號申請