綠色資源網:您(nín)身邊最(zuì)放心的安全(quán)下載站! 最新軟件|熱門排行|軟件分(fèn)類|軟件(jiàn)專題|廠商大全

綠色(sè)資源網

技術教程
您的(de)位置:首頁數據庫類Oracle → Oracle數據庫如何(hé)查找刪除重複的SQL語句

Oracle數據(jù)庫如何查找刪(shān)除重複(fù)的(de)SQL語句

我要(yào)評論 2011/05/27 15:13:25 來源(yuán):綠色資源網 編輯:chithemodel.com [大(dà) 中(zhōng) ] 評論:0 點擊:496次

查詢及刪除重複記錄的SQL語句

  1、查找表中多餘(yú)的重複記錄,重複記錄是根(gēn)據(jù)單個字段(peopleId)來判斷

  select * from people

  where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

  2、刪(shān)除(chú)表中多餘的重複記錄,重(chóng)複記錄是根據單(dān)個字(zì)段(peopleId)來判斷,隻留有 rowid最小的記錄

  delete from people

  where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

  and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)

  注:rowid為oracle自帶不用該.....

  3、查找表中多餘的重複記錄(多個字段)

  select * from vitae a

  where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

  4、刪除表中多餘的(de)重複記錄(多個(gè)字段(duàn)),隻留有(yǒu)rowid最小的記(jì)錄

  delete from vitae a

  where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

  and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

  5、查找(zhǎo)表中多餘(yú)的重複記錄(多個(gè)字(zì)段),不包含rowid最小(xiǎo)的記錄

  select * from vitae a

  where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

  and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

  (二)

  比方說

  在A表中存在一個字段(duàn)“name”,

  而且(qiě)不同(tóng)記(jì)錄之間的“name”值有可(kě)能會相同,

  現在(zài)就是需要查詢(xún)出在(zài)該表中的各記錄之間,“name”值存在重(chóng)複的項;

  Select Name,Count(*) From A Group By Name Having Count(*) > 1

  如果還查性別也相同大則(zé)如下:

  Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1

  (三)

  方(fāng)法一(yī)

  declare @max integer,@id integer

  declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 1

  open cur_rows

  fetch cur_rows into @id,@max

  while @@fetch_status=0

  begin

  select @max = @max -1

  set rowcount @max

  delete from 表名 where 主字(zì)段 = @id

  fetch cur_rows into @id,@max

  end

  close cur_rows

  set rowcount 0 方法二

  "重複記錄"有兩個(gè)意義(yì)上的重複記錄,一是完全重複的記錄,也即所有字段均(jun1)重複的記錄,二是部分關鍵字(zì)段重複的(de)記錄,比(bǐ)如Name字(zì)段重(chóng)複(fù),而(ér)其他字段不一定重(chóng)複或(huò)都(dōu)重複可以(yǐ)忽(hū)略。

  1、對於第一種重複,比較容易解決,使(shǐ)用

  select distinct * from tableName

  就可以得到無重複記錄的結果集。

  如果該表需要(yào)刪除重複的記(jì)錄(重複(fù)記(jì)錄(lù)保留1條(tiáo)),可以按(àn)以(yǐ)下方法刪除

  select distinct * into #Tmp from tableName

  drop table tableName

  select * into tableName from #Tmp

  drop table #Tmp

  發生這種(zhǒng)重複(fù)的原(yuán)因是表(biǎo)設計不周產生(shēng)的,增加唯一索引列即可解(jiě)決。

  2、這類重複問題通常要求保留重(chóng)複(fù)記錄中(zhōng)的第(dì)一條記(jì)錄,操作方法如下

  假設(shè)有重複的字段(duàn)為(wéi) Name,Address,要求(qiú)得到(dào)這兩個字段(duàn)唯一的結果(guǒ)集(jí)

  select identity(int,1,1) as autoID, * into #Tmp from tableName

  select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID

  select * from #Tmp where autoID in(select autoID from #tmp2)

  最後一個select即得到了 Name,Address不重複的結果集(但多(duō)了一(yī)個autoID字段,實(shí)際寫時可以寫在select子(zǐ)句中省(shěng)去此列)

  (四)

  查詢重複

  select * from tablename where id in (

  select id from tablename

  group by id

  having count(id) > 1

  )

關鍵詞(cí):Oracle,數(shù)據庫,SQL語句(jù)

閱讀本文後您有什麽感想? 已有 人給(gěi)出(chū)評(píng)價!

  • 0 歡迎喜歡
  • 0 白癡
  • 0 拜托
  • 0 哇
  • 0 加油(yóu)
  • 0 鄙視