Delphi .NET (2) Database (71) Delphi IDE (89) Network (39) Printing (3) Strings (12) VCL (83) Windows with Delphi (280)
Exchange Links About this site Links to us 
|
Find duplicate rows in a table (no primary key)
22 comments. Current rating: (8 votes). Leave comments and/ or rate it.
Question:
I have a table of city names and need to find duplicate entries. The table does not have a primary key, so the duplicate rows are entirely identical.
Answer:
You can use the GROUP clause in your SELECT statement. Below are 3 different attempts. Try the first one first. It may not work with your DBMS but if it works, it's most convenient. The second one is an ok choice as it returns all rows but the duplicate rows in the 'See Also' box.
The last version returns all city names with a count value and you manually have to look for those with a value > 1. If you need to do this more often, you should consider creating a VIEW or a temporary table with this query and then do a SELECT on this VIEW/ temp table.
In Microsoft SQL-Server, you can use the HAVING clause, as shown at the bottom.
And: If you do have a primary key defined, see the other tip mentioned at the top. This would work in that situation, but using the unique ID is better.
 | |  | |
select city_name, count(city_name) as cnt
from areas
group by city_name
where cnt>1
select city_name, count(city_name) as cnt
from areas
group by city_name
order by cnt desc
select city_name, count(city_name) as cnt
from areas
group by city_name
select city_name
from areas
group by city_name
having count(*) > 1
| |  | |  |
Comments:
| You are on page 1 of 2, other pages: [1] 2 | |
anonymous from India
|
 |
|
|
|
|
anonymous from Czech Republic
|
|
|
|
it's better
SELECT A.ID, B.ID
FROM
T1 A
INNER JOIN T1 B ON (A.F1=B.F1 AND A.F2 AND B.F2 AND ...)
WHERE A.ID<B.ID
|
|
anonymous from India
|
 |
|
|
|
|
anonymous from India
|
|
|
|
When you want to find duplicate combination of multiple columns,
group by is the best approach.
SELECT col1, col2,col3,count(col3)--put the last column of duplicate combination in Count()
FROM table1
GROUP BY col1,col2,col3
Here as col1,col2,col3 combination will be unique as they are grouped by GROUP BY clause and count() returns the count. We can add a HAVING Clause to filter combinations for whom the count() is > 1
SELECT col1, col2,col3,count(col3)--put the last column of duplicate combination in Count()
FROM table1
GROUP BY col1,col2,col3
HAVING COUNT(col3) >1
|
|
anonymous from Switzerland
|
|
|
|
Use full
|
|
anonymous from Germany
|
 |
|
|
used it in MySQL and it worked great. very helpful, thanks
|
|
anonymous from United States
|
 |
|
|
I have a slightly different need.
I have a table that carries multiple rows for a single key.
Example:
Two cities in same state. (Cities Marlboro, Middleboro in State of Califernia)
My need is to show one row per state and cancatenate cities in that row, e.g.,
Califernia, Marlboro and Middleboro
Any suggestions on how to do this?
|
|
anonymous from India
|
|
|
|
I have a slightly different need.
I have a table that carries multiple rows for a single key.
Example:
Two cities in same state. (Cities Marlboro, Middleboro in State of Califernia)
My need is to show one row per state and cancatenate cities in that row, e.g.,
Califernia, Marlboro and Middleboro
Any suggestions on how to do this?
Your Solution can be:
SELECT state1 . * , state2.name
FROM `state` state1
LEFT JOIN state state2 ON state1.statename = state2.statename
WHERE state1.city <> state2.city
GROUP BY state1.statename
|
|
anonymous from India
|
|
|
|
Good ,It worked for me too. thanks.
|
|
|
|
|
Can someone help me with my problem?
My table looks like this
CID FNAME MIN MAX COM
A OP1 0 23 5
A OP1 24 35 2
A OP1 36 99 1
A OP2 0 23 5
A OP2 24 35 2
A OP2 36 99 0
A OP3 0 23 5
A OP3 24 35 2
A OP3 36 99 1
B OP1 0 23 9
B OP1 24 99 2
B OP2 0 23 9
B OP2 24 99 2
B OP3 0 23 7
B OP3 24 35 3
B OP3 36 99 1
The expected duplicate results i need are:
A OP1
A OP3
B OP1
B OP2
|
| You are on page 1 of 2, other pages: [1] 2 |
|