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

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


当前回答

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

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

其他回答

1. 解决方案

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

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。

下面是一个SQL请求:

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

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

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

如何:

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

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

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