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

时间:2008-09-12 15:10:27

标签: sql oracle duplicate-data

什么是最简单的SQL语句,它将返回给定列的重复值及其在Oracle数据库表中的出现次数?

例如:我有一个JOBS表,其中包含JOB_NUMBER列。我怎样才能知道我是否有任何重复的JOB_NUMBER,以及它们被复制了多少次?

13 个答案:

答案 0 :(得分:560)

SELECT column_name, COUNT(column_name)
FROM table_name
GROUP BY column_name
HAVING COUNT(column_name) > 1;

答案 1 :(得分:54)

另一种方式:

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

column_name上有索引时工作正常(足够快)。并且它是删除或更新重复行的更好方法。

答案 2 :(得分:31)

最简单的我能想到:

select job_number, count(*)
from jobs
group by job_number
having count(*) > 1;

答案 3 :(得分:16)

如果您不需要知道重复的实际数量,则不需要在返回的列中包含计数。 e.g。

SELECT column_name
FROM table
GROUP BY column_name
HAVING COUNT(*) > 1

答案 4 :(得分:7)

怎么样:

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

要回答上面的例子,它看起来像:

SELECT job_number, count(*)
FROM jobs
GROUP BY job_number HAVING COUNT(*) > 1;

答案 5 :(得分:5)

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

使用行ID   例如emp_dept(empid,deptid,startdate,enddate)    假设empid和deptid是唯一的,并在那种情况下识别行

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);

答案 6 :(得分:4)

否则

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

将为您提供重复的行ID。

答案 7 :(得分:4)

SELECT   SocialSecurity_Number, Count(*) no_of_rows
FROM     SocialSecurity 
GROUP BY SocialSecurity_Number
HAVING   Count(*) > 1
Order by Count(*) desc 

答案 8 :(得分:1)

我通常使用Oracle Analytic函数ROW_NUMBER()

假设您要检查有关在列(c1c2c3)上构建的唯一索引或主键的重复项。 然后你会这样做,提出 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)

答案 9 :(得分:1)

我知道它是一个旧线程,但这可能会帮助一些人。

如果您需要在检查以下重复使用时打印表格的其他列:

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子句中添加一些其他过滤器。

答案 10 :(得分:0)

这是执行此操作的SQL请求:

select column_name, count(1)
from table
group by column_name
having count (column_name) > 1;

答案 11 :(得分:-1)

此外,您可以尝试这样的方法列出表格中的所有重复值,例如reqitem

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;

答案 12 :(得分:-1)

<强> 1。溶液

select * from emp
    where rowid not in
    (select max(rowid) from emp group by empno);