5

Sql Query to find duplicates in 2 columns where the values ​​in the first column...

 3 years ago
source link: https://www.codesd.com/item/sql-query-to-find-duplicates-in-2-columns-where-the-values-in-the-first-column-are-the-same.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Sql Query to find duplicates in 2 columns where the values ​​in the first column are the same

advertisements

I have a table where the first column contains States and second column contains Zip Code. I want to find duplicate Zip Codes in the same State. So, the first column can have same values but i need to find the duplicates in the second column that have the same values in the first column.

Table :

+---+----+------+
| Z | A  |  B   |
+---+----+------+
| 1 | GA | 1234 |
| 2 | GA | 321  |
| 3 | GA | 234  |
| 4 | GA | 9890 |
| 5 | GA | 1234 |
+---+----+------+

The query should return the value of the zip code that has a duplicate i.e 1234. I have around 10000+ records.

Thank You.


Try using a GROUP BY query and retain zip codes appearing in duplicate.

SELECT A, B
FROM yourTable
GROUP BY A, B
HAVING COUNT(*) > 1

Note that we can group by state and zip code assuming that a given zip code only appears once, for a given state.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK