sql - 如何在Oracle中的表中找到重複值?
什麼是最簡單的SQL語句,它将傳回給定列的重複值及其在Oracle資料庫表中的出現次數?
例如:我有一個JOBS表,其中包含222196339345876684列。如何檢視是否有任何重複的JOB_NUMBER,以及它們被複制了多少次?
13個解決方案
519 votes
select column_name, count(column_name)
from table
group by column_name
having count (column_name) > 1;
Bill the Lizard answered 2019-02-17T04:17:40Z
47 votes
其他方式:
SELECT *
FROM TABLE A
WHERE EXISTS (
SELECT 1 FROM TABLE
WHERE COLUMN_NAME = A.COLUMN_NAME
AND ROWID < A.ROWID
)
當column_name上有索引時工作正常(足夠快)。并且它是删除或更新重複行的更好方法。
Grrey answered 2019-02-17T04:18:07Z
30 votes
最簡單的我能想到:
select job_number, count(*)
from jobs
group by job_number
having count(*) > 1;
JosephStyons answered 2019-02-17T04:18:32Z
16 votes
如果您不需要知道重複的實際數量,則不需要在傳回的列中包含計數。 例如
SELECT column_name
FROM table
GROUP BY column_name
HAVING COUNT(*) > 1
Evan answered 2019-02-17T04:18:58Z
7 votes
怎麼樣:
SELECT , count(*)
FROM
GROUP BY HAVING COUNT(*) > 1;
要回答上面的例子,它看起來像:
SELECT job_number, count(*)
FROM jobs
GROUP BY job_number HAVING COUNT(*) > 1;
Andrew answered 2019-02-17T04:19:25Z
5 votes
如果多列辨別唯一行(例如關系表),則可以使用以下内容
使用行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);
Jitendra Vispute answered 2019-02-17T04:20:06Z
4 votes
幹
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。
agnul answered 2019-02-17T04:20:33Z
4 votes
SELECT SocialSecurity_Number, Count(*) no_of_rows
FROM SocialSecurity
GROUP BY SocialSecurity_Number
HAVING Count(*) > 1
Order by Count(*) desc
Wahid Haidari answered 2019-02-17T04:20:52Z
1 votes
我知道這是一個舊線程,但這可能對某人有所幫助。
如果您需要列印表格的其他列,同時檢查下面的重複使用:
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子句中添加一些額外的過濾器。
Parth Kansara answered 2019-02-17T04:21:31Z
0 votes
我通常使用Oracle Analytic函數ROW_NUMBER()。
假設您要檢查有關在列上建構的唯一索引或主鍵的重複項(ROWID,c2,c3)。然後你會這樣,帶來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)
J. Chomel answered 2019-02-17T04:22:05Z
0 votes
這是執行此操作的SQL請求:
select column_name, count(1)
from table
group by column_name
having count (column_name) > 1;
Chaminda Dilshan answered 2019-02-17T04:22:32Z
-1 votes
解決方案
select * from emp
where rowid not in
(select max(rowid) from emp group by empno);
DoOrDie answered 2019-02-17T04:22:58Z
-1 votes
你也可以嘗試這樣的方法來列出表格中的所有重複值,例如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;
Stacker answered 2019-02-17T04:23:25Z