Answers:
SELECT column_name, COUNT(column_name)
FROM table_name
GROUP BY column_name
HAVING COUNT(column_name) > 1;
group by
,例如:select column_one, column_two, count(*) from tablename group by column_one, column_two having count(column_one) > 1;
等
having count(*) > 1
:D
其他方式:
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;
如果多个列标识唯一行(例如,关系表),则可以使用以下命令
使用行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 SocialSecurity_Number, Count(*) no_of_rows
FROM SocialSecurity
GROUP BY SocialSecurity_Number
HAVING Count(*) > 1
Order by Count(*) desc
我通常使用Oracle Analytic函数ROW_NUMBER()。
假设你要检查你有关于一个唯一索引或建立在列的主键(重复的c1
,c2
,c3
)。然后,您将采用这种方式,调出ROWID
s行,其中带来的行数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)
这是执行此操作的SQL请求:
select column_name, count(1)
from table
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子句中添加一些其他过滤器。