2

I tried this with solutions avaialble online, but none worked for me.

Table :
Id rank 
1  100
1  100
2  75
2  45
3  50
3 50

I want Ids 1 and 3 returned, beacuse they have duplicates.

I tried something like

 select * from A where rank in ( 
 select rank from A group by rank having count(rank) > 1

This also returned ids without any duplicates. Please help.

user393148
  • 897
  • 4
  • 16
  • 27
  • http://stackoverflow.com/questions/4010311/how-to-check-if-all-fields-are-unique-in-oracle – tyjkenn Feb 28 '12 at 02:58
  • Duplicate question, the irony! - http://stackoverflow.com/questions/2053569/is-it-possible-for-sql-to-find-records-with-duplicates – J Cooper Feb 28 '12 at 03:10
  • @JCooper: Sorry. I looked up online before posting. Tried couple. But didnt know I need to group by both id and rank. – user393148 Feb 28 '12 at 03:16

3 Answers3

4

Try this:

select id from table
group by id, rank
having count(*) > 1
Mosty Mostacho
  • 42,742
  • 16
  • 96
  • 123
1
select id, rank
from
(
    select id, rank, count(*) cnt
    from rank_tab
    group by id, rank
    having count(*) > 1
) t
Thit Lwin Oo
  • 3,388
  • 3
  • 20
  • 23
1

This general idea should work:

SELECT id
FROM your_table
GROUP BY id
HAVING COUNT(*) > 1 AND COUNT(DISTINCT rank) = 1

In plain English: get every id that exists in multiple rows, but all these rows have the same value in rank.


If you want ids that have some duplicated ranks (but not necessarily all), something like this should work:

SELECT id
FROM your_table
GROUP BY id
HAVING COUNT(*) > COUNT(DISTINCT rank)
Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167