这个工程。在SQL Server中运行它是因为甲骨文在Fiddle上坏了,但是应该在任一系统上工作。
的MS SQL Server 2014架构设置:
CREATE TABLE ab
([id] int, [cost] int)
;
INSERT INTO ab
([id], [cost])
VALUES
(1, 5),
(2, 5),
(3, 15),
(3, 15),
(4, 24),
(5, 68),
(6, 13),
(7, 3)
;
查询1:
with a1 as (
SELECT id
,cost
,rank() over (partition by cost order by id) dup
From ab
)
select * from a1 where dup > 1
Results:
| id | cost | dup |
|----|------|-----|
| 2 | 5 | 2 |
然后返回那里有一个重复的成本都值:
with a1 as (
SELECT id
,cost
,rank() over (partition by cost order by id) dup
From ab
)
,a2 as (select * from a1 where dup > 1)
select * from ab
join a2 on ab.cost = a2.cost
您是否尝试在内部where子句中添加'a.id!= b.id'? –
Madhusudna这是sql所以操作符不等于由<>显示。你不能使用!=。 –