zoukankan      html  css  js  c++  java
  • Oracle:sql语句查询没有重复的记录数目

    1、通过创建临时表

    可以把数据先导入到一个临时表中,然后删除原表的数据,再把数据导回原表,SQL语句如下:

     creat table tbl_tmp (select distinct* from tbl); truncate table tbl;//清空表记录 insert into tbl select * from tbl_tmp; //将临时表中的数据插回来。

    这种方法可以实现需求,但是很明显,对于一个千万级记录的表,这种方法很慢,在生产系统中,这会给系统带来很大的开销,不可行。

    2、利用rowid

    在oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,rowid确定了每条记录是oracle中的哪一个数据文件、块、行上。在重复的记录中,可能所有列的内容都相同,但rowid不会相同。SQL语句如下:

     delete from tbl where rowid in ( select a.rowid from tbl a, tbl b where a.rowid>b.rowid and a.col1=b.col1 and a.col2 = b.col2)

    如果已经知道每条记录只有一条重复的,这个sql语句适用。但是如果每条记录的重复记录有N条,这个N是未知的,就要考虑适用下面这种方法了。

    3、利用max或min函数

    这里也要使用rowid,与上面不同的是结合max或min函数来实现。SQL语句如下

     delete from tbl awhere rowid not in ( select max(b.rowid) from tbl b where a.col1=b.col1 and a.col2 = b.col2); //这里max使用min也可以 或者用下面的语句 delete from tbl awhere rowid<(select max(b.rowid) from tbl b where a.col1=b.col1 and a.col2 = b.col2); //这里如果把max换成min的话,前面的where子句中需要把"<"改为">"

    跟上面的方法思路基本是一样的,不过使用了group by,减少了显性的比较条件,提高效率。SQL语句如下:

     deletefrom tbl where rowid not in (select max(rowid) from tbl tgroup by t.col1, t.col2); delete from tbl where (col1, col2) in (select col1,col2 from tbl group bycol1,col2 havingcount(*) >1) and rowid not in (select nin(rowid) from tbl group by col1,col2 having count(*) >1)

    还有一种方法,对于表中有重复记录的记录比较少的,并且有索引的情况,比较适用。假定col1,col2上有索引,并且tbl表中有重复记录的记录比较少,SQL语句如下4、利用group by,提高效率

    4、Group By方法

    一、查数据
    Select count(Num) ,max(Name) from student –列出重复记录,并列出属性
    Group By Name
    Having count(Num) >1 –按Name分组后找出表中Num列重复,即出现次数大于1
    二、删数据
    Delete from student Group By Name Having count(Num) > 1
    (一)
    1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断

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

    2、删除表中多余的重复记录,重复记录是根据单个字段(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)

    3、查找表中多余的重复记录(多个字段)

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

    4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录

     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、查找表中多余的重复记录(多个字段),不包含rowid最小的记录

     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)

    select g.country,count(DISTINCT(g.GENE_ID)) as gene_count from (select p.gene_id,upper(t.country) as country
    from medline_citation t,tb_gene_2pubmed p
    where t.pmid=p.pubmed_id and t.country is not null ) g group by g.country

  • 相关阅读:
    ASP.NET HttpRuntime.Cache缓存类使用总结
    ASP.NET MVC自定义AuthorizeAttribute篇知识点讲解—登录限制
    Echarts图表控件使用总结2(Line,Bar)—问题篇
    数据库查询实例(包含所有where条件例子)
    php file_get_contents读取大容量文件方法
    如何给mysql用户分配权限
    dedecms {dede:php}标签用法介绍
    js获取字符串最后一个字符代码
    CSS3选择器之学习笔记
    SQL中实现SPLIT函数几种方法
  • 原文地址:https://www.cnblogs.com/xuewater/p/2810108.html
Copyright © 2011-2022 走看看