Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: How to find duplicates based on two fields?

Tags:

I have rows in an Oracle database table which should be unique for a combination of two fields but the unique constrain is not set up on the table so I need to find all rows which violate the constraint myself using SQL. Unfortunately my meager SQL skills aren't up to the task.

My table has three columns which are relevant: entity_id, station_id, and obs_year. For each row the combination of station_id and obs_year should be unique, and I want to find out if there are rows which violate this by flushing them out with an SQL query.

I have tried the following SQL (suggested by this previous question) but it doesn't work for me (I get ORA-00918 column ambiguously defined):

SELECT entity_id, station_id, obs_year FROM mytable t1 INNER JOIN ( SELECT entity_id, station_id, obs_year FROM mytable  GROUP BY entity_id, station_id, obs_year HAVING COUNT(*) > 1) dupes  ON  t1.station_id = dupes.station_id AND t1.obs_year = dupes.obs_year 

Can someone suggest what I'm doing wrong, and/or how to solve this?

like image 919
James Adams Avatar asked Aug 17 '10 15:08

James Adams


People also ask

How do I find duplicate fields in SQL?

One way to find duplicate records from the table is the GROUP BY statement. The GROUP BY statement in SQL is used to arrange identical data into groups with the help of some functions. i.e if a particular column has the same values in different rows then it will arrange these rows in a group.

How do I check if two columns have the same value in SQL?

In SQL, problems require us to compare two columns for equality to achieve certain desired results. This can be achieved through the use of the =(equal to) operator between 2 columns names to be compared.


1 Answers

SELECT  * FROM    (         SELECT  t.*, ROW_NUMBER() OVER (PARTITION BY station_id, obs_year ORDER BY entity_id) AS rn         FROM    mytable t         ) WHERE   rn > 1 
like image 119
Quassnoi Avatar answered Sep 18 '22 08:09

Quassnoi