代码之家  ›  专栏  ›  技术社区  ›  jitbit

如何在Oracle中的表中查找重复值?

  •  244
  • jitbit  · 技术社区  · 16 年前

    例如:我有一个 JOBS 带有列的表 JOB_NUMBER . 我怎样才能知道我是否有副本 工作编号 s、 它们被复制了多少次?

    13 回复  |  直到 12 年前
        1
  •  647
  •   Bill the Lizard Alexis MP    4 年前

    按计数聚合列,然后使用HAVING子句查找大于一次的值。

    SELECT column_name, COUNT(column_name)
    FROM table_name
    GROUP BY column_name
    HAVING COUNT(column_name) > 1;
    
        2
  •  59
  •   Bill the Lizard Alexis MP    12 年前

    另一种方式:

    SELECT *
    FROM TABLE A
    WHERE EXISTS (
      SELECT 1 FROM TABLE
      WHERE COLUMN_NAME = A.COLUMN_NAME
      AND ROWID < A.ROWID
    )
    

    column_name . 这是删除或更新重复行的更好方法。

        3
  •  35
  •   JosephStyons    16 年前

    我能想到的最简单的方法是:

    select job_number, count(*)
    from jobs
    group by job_number
    having count(*) > 1;
    
        4
  •  17
  •   Evan    16 年前

    如果不需要知道副本的实际数量,则甚至不需要在返回的列中包含计数。例如

    SELECT column_name
    FROM table
    GROUP BY column_name
    HAVING COUNT(*) > 1
    
        5
  •  7
  •   jitbit    16 年前

    怎么样:

    SELECT <column>, count(*)
    FROM <table>
    GROUP BY <column> HAVING COUNT(*) > 1;
    

    SELECT job_number, count(*)
    FROM jobs
    GROUP BY job_number HAVING COUNT(*) > 1;
    
        6
  •  6
  •   Jitendra Vispute    12 年前

    如果多列标识唯一行(例如关系表),则可以使用以下命令

    使用行id e、 g.环境管理部(环境管理署、环境管理署、开始日期、结束日期)

    select oed.empid, count(oed.empid) 
    from emp_dept oed 
    where exists ( select * 
                   from  emp_dept ied 
                    where oed.rowid <> ied.rowid and 
                           ied.empid = oed.empid and 
                          ied.deptid = oed.deptid )  
            group by oed.empid having count(oed.empid) > 1 order by count(oed.empid);
    

    如果这样的表有主键,那么使用主键而不是rowid,例如id是pk,那么

    select oed.empid, count(oed.empid) 
    from emp_dept oed 
    where exists ( select * 
                   from  emp_dept ied 
                    where oed.id <> ied.id and 
                           ied.empid = oed.empid and 
                          ied.deptid = oed.deptid )  
            group by oed.empid having count(oed.empid) > 1 order by count(oed.empid);
    
        7
  •  4
  •   agnul    16 年前

    select count(j1.job_number), j1.job_number, j1.id, j2.id
    from   jobs j1 join jobs j2 on (j1.job_numer = j2.job_number)
    where  j1.id != j2.id
    group by j1.job_number
    

        8
  •  4
  •   Simon Adcock    11 年前
    SELECT   SocialSecurity_Number, Count(*) no_of_rows
    FROM     SocialSecurity 
    GROUP BY SocialSecurity_Number
    HAVING   Count(*) > 1
    Order by Count(*) desc 
    
        9
  •  4
  •   J. Chomel    7 年前

    我通常使用 Oracle Analytic 作用 ROW_NUMBER() .

    c1 , c2 c3 ). 那你就走这条路,带着 ROWID 行的数目,其中 ROW_NUMBER() >1 :

    Select * From Table_With_Duplicates
          Where Rowid In
                        (Select Rowid
                           From (Select Rowid,
                                        ROW_NUMBER() Over (
                                                Partition By c1 || c2 || c3
                                                Order By c1 || c2 || c3
                                            ) nbLines
                                   From Table_With_Duplicates) t2
                          Where nbLines > 1)
    
        10
  •  3
  •   Parth Kansara    6 年前

    如果在检查是否重复使用时需要打印表的其他列,请执行以下操作:

    select * from table where column_name in
    (select ing.column_name from table ing group by ing.column_name having count(*) > 1)
    order by column_name desc;
    

    如果需要,还可以在where子句中添加一些附加过滤器。

        11
  •  1
  •   typedef    7 年前

    select column_name, count(1)
    from table
    group by column_name
    having count (column_name) > 1;
    
        12
  •  0
  •   DoOrDie    8 年前

    1.解决方案

    select * from emp
        where rowid not in
        (select max(rowid) from emp group by empno);
    
        13
  •  -1
  •   Yaron Idan    7 年前

    SELECT count(poid) 
    FROM poitem 
    WHERE poid = 50 
    AND rownum < any (SELECT count(*)  FROM poitem WHERE poid = 50) 
    GROUP BY poid 
    MINUS
    SELECT count(poid) 
    FROM poitem 
    WHERE poid in (50)
    GROUP BY poid 
    HAVING count(poid) > 1;