這個工程。在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所以操作符不等於由<>顯示。你不能使用!=。 –