298

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

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

4

13 回答 13

653

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

SELECT column_name, COUNT(column_name)
FROM table_name
GROUP BY column_name
HAVING COUNT(column_name) > 1;
于 2008-09-12T15:13:46.583 回答
61

其他方式:

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

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

于 2008-09-13T09:35:41.800 回答
35

我能想到的最简单的:

select job_number, count(*)
from jobs
group by job_number
having count(*) > 1;
于 2008-09-12T15:17:14.577 回答
17

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

SELECT column_name
FROM table
GROUP BY column_name
HAVING COUNT(*) > 1
于 2008-09-13T14:55:49.440 回答
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;
于 2008-09-12T15:18:28.817 回答
6

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

使用行 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);
于 2012-09-20T07:25:14.333 回答
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。

于 2008-09-12T15:24:34.997 回答
4
SELECT   SocialSecurity_Number, Count(*) no_of_rows
FROM     SocialSecurity 
GROUP BY SocialSecurity_Number
HAVING   Count(*) > 1
Order by Count(*) desc 
于 2013-04-05T06:48:51.973 回答
4

我通常使用Oracle 分析函数ROW_NUMBER()

假设您要检查有关基于列 ( c1, c2, c3) 构建的唯一索引或主键的重复项。然后你会走这条路,调出ROWIDs 行,其中行数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)
于 2017-10-24T08:21:15.677 回答
3

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

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

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 子句中添加一些额外的过滤器。

于 2018-07-23T07:57:48.033 回答
1

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

select column_name, count(1)
from table
group by column_name
having count (column_name) > 1;
于 2018-01-12T11:02:40.773 回答
0

1.解决方案

select * from emp
    where rowid not in
    (select max(rowid) from emp group by empno);
于 2016-02-10T10:08:45.063 回答
-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;
于 2016-01-27T14:23:14.090 回答