如何在Oracle的表中find重复值?
什么是最简单的SQL语句,将返回给定列的重复值以及Oracle数据库表中出现的次数?
例如:我有JOB_NUMBER
列的JOBS
表。 我如何知道我是否有任何重复的JOB_NUMBER
,以及它们重复了多less次?
select column_name, count(column_name) from table group by column_name having count (column_name) > 1;
其他方式:
SELECT * FROM TABLE A WHERE EXISTS ( SELECT 1 FROM TABLE WHERE COLUMN_NAME = A.COLUMN_NAME AND ROWID < A.ROWID )
当column_name
上有索引时,工作正常(足够快)。 这是删除或更新重复行的更好方法。
最简单的我可以想到:
select job_number, count(*) from jobs group by job_number having count(*) > 1;
如果您不需要知道重复的实际数量,您甚至不需要在返回的列中进行计数。 例如
SELECT column_name FROM table GROUP BY column_name HAVING COUNT(*) > 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;
在多列标识唯一行(例如关系表)的情况下,可以使用以下内容
使用行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);
干
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。
SELECT SocialSecurity_Number, Count(*) no_of_rows FROM SocialSecurity GROUP BY SocialSecurity_Number HAVING Count(*) > 1 Order by Count(*) desc
我通常使用Oracle分析函数ROW_NUMBER() 。
假设您想检查您拥有的关于build立在列( c1
, c2
, c3
)上的唯一索引或主键的重复项。 那么你ROWID
,提出了ROW_NUMBER()
带来的行数>1
的行的ROWID
。
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)
解决scheme
select * from emp where rowid not in (select max(rowid) from emp group by empno);
另外你可以尝试这样的事情列出所有重复的值在表中说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;