Oracle SQL:查找重复的行
我有下表Oracle SQL:查找重复的行,sql,Sql,我有下表 itemId Name PartNum Price 1 apple 123 0.99 2 orange 234 0.5 3 apple 123 0.99 我想找到重复的行。它应该输出 ItemId Name PartNum Price 1 apple 123 0.99 3 apple 123 0.99 如何做????现在我明白了,你
itemId Name PartNum Price
1 apple 123 0.99
2 orange 234 0.5
3 apple 123 0.99
我想找到重复的行。它应该输出
ItemId Name PartNum Price
1 apple 123 0.99
3 apple 123 0.99
如何做????现在我明白了,你可以这样做:
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)
现在我明白了,你可以这样做:
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)
有两种方法可以实现这一点,基本上就是将表本身连接起来。下面是一个使用
公共表表达式
和rank()
函数的解决方案:
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
以下是另一种方法:
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
有几种方法可以实现这一点,基本上就是将表本身连接起来。下面是一个使用
公共表表达式
和rank()
函数的解决方案:
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
以下是另一种方法:
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
克劳迪奥的答案非常接近,但要根据重复数过滤结果,您需要添加having子句:
select name, partnum, price
from yourTable
group by name, partnum, price
having count(itemId) > 1
Claudio的答案非常接近,但要根据重复数过滤结果,您需要添加having子句:
select name, partnum, price
from yourTable
group by name, partnum, price
having count(itemId) > 1
这是另一种选择
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
检查它,这是另一种选择
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
检查一下,这是另一种方法: 查询:
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 |
:
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 |
这是另一种方法: 查询:
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 |
:
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 |
这里有另一种方法。这使用分析函数
count(*)
这里有另一种方法。这使用分析函数
count(*)
这不包括OP需要的
itemid
列。我认为您需要将表连接回自身以获得该值。这不包括OP需要的itemid
列。我认为您需要将表连接回自身以获得该值。