[英]Oracle SQL: find duplicated row
I have following table 我有下表
itemId Name PartNum Price
1 apple 123 0.99
2 orange 234 0.5
3 apple 123 0.99
I want to find the duplicated rows. 我想找到重复的行。 It should output
它应该输出
ItemId Name PartNum Price
1 apple 123 0.99
3 apple 123 0.99
How to do it??????? 怎么做???????
There are a couple ways to do this basically around joining the table to itself. 基本上有两种方法可以将表连接到自身上。 Here's one solution using a
common table expression
with the rank()
function: 这是使用带有
rank()
函数的common table expression
解决方案:
with cte as (
select itemId,
name,
partnum,
price,
rank() over (order by name, partnum, price) rnk
from yourtable
)
select distinct c.*
from cte c
join cte c2 on c.rnk = c2.rnk and c.itemid != c2.itemid
Here's an alternative approach: 这是另一种方法:
select distinct y.*
from yourtable y
join yourtable y2 on
y.name = y2.name and
y.partnum = y2.partnum and
y.price = y2.price and
y.itemid != y2.itemid
Now I understand and you can do this: 现在我了解了,您可以执行以下操作:
select * from yourTable where name in (
select name from (
SELECT Name, PartNum, Price, count(ItemId) qtd
FROM yourTable
group by Name, PartNum, Price,)
where qtd>1)
Claudio's answer is pretty close, but to filter the results based on the number of duplicates, you'll want to add a having clause: Claudio的答案非常接近,但是要根据重复项的数量过滤结果,您需要添加一个having子句:
select name, partnum, price
from yourTable
group by name, partnum, price
having count(itemId) > 1
This is another approach : 这是另一种方法:
Query : 查询 :
select *
from Table1
where Name||','||PartNum in (
select Name||','||PartNum
from Table1
group by Name, PartNum
having count(*) > 1)
| ITEMID | NAME | PARTNUM | PRICE |
|--------|-------|---------|-------|
| 1 | apple | 123 | 0.99 |
| 3 | apple | 123 | 0.99 |
This is another alternative 这是另一种选择
SELECT t1.itemId, t1.name, t1.partNum, t1.price
FROM table1 t1
INNER JOIN (SELECT name, partNum, price, COUNT(*) AS count
FROM table1
GROUP BY name, partNum, price
HAVING COUNT(*) > 1
) dt ON t1.name = dt.name and t1.partNum = dt.partNum
and t1.price = dt.price
ORDER BY t1.itemId
Check it on SQL FIDDLE 在SQL FIDDLE上检查
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.