在Oracle数据库表中返回给定列的重复值及其出现次数的最简单SQL语句是什么?
例如:我有一个列为JOB_NUMBER的JOBS表。如何才能知道我是否有任何重复的JOB_NUMBERs,以及它们重复了多少次?
在Oracle数据库表中返回给定列的重复值及其出现次数的最简单SQL语句是什么?
例如:我有一个列为JOB_NUMBER的JOBS表。如何才能知道我是否有任何重复的JOB_NUMBERs,以及它们重复了多少次?
当前回答
SELECT SocialSecurity_Number, Count(*) no_of_rows
FROM SocialSecurity
GROUP BY SocialSecurity_Number
HAVING Count(*) > 1
Order by Count(*) desc
其他回答
下面是一个SQL请求:
select column_name, count(1)
from table
group by column_name
having count (column_name) > 1;
我能想到的最简单的:
select job_number, count(*)
from jobs
group by job_number
having count(*) > 1;
按COUNT聚合列,然后使用HAVING子句查找出现多次的值。
SELECT column_name, COUNT(column_name)
FROM table_name
GROUP BY column_name
HAVING COUNT(column_name) > 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子句中添加一些额外的过滤器。
1. 解决方案
select * from emp
where rowid not in
(select max(rowid) from emp group by empno);