在2个字段中查找重复的行?

时间:2016-11-07 17:01:08

标签: sql

您能教我如何在不同的行中找到重复或相同的值吗?

SELECT * FROM `geoname`

现在我需要将这些行与重复项进行比较:

WHERE `feature code` = 'ADM1'

WHERE `feature code` = 'ADM2'

更新。

列NAME包含可以重复的名称。我想找一个?

2 个答案:

答案 0 :(得分:2)

要计算这两个特征代码的出现次数,您可以尝试条件聚合:

SELECT SUM(CASE WHEN `feature code` = 'ADM1' THEN 1 ELSE 0 END) AS adm1count,
       SUM(CASE WHEN `feature code` = 'ADM2' THEN 1 ELSE 0 END) AS adm2count
FROM yourTable

更新:如果您想查找重复出现的名称,可以尝试:

SELECT NAME
FROM yourTable
GROUP BY NAME
HAVING COUNT(*) > 1

答案 1 :(得分:1)

SELECT geo.*,count(*) as Duplicate_Count
   FROM dbo.geoname AS geo
   having count(*) > 1
   group by geo.*
WHERE `feature code` = 'ADM1' OR `feature code` = 'ADM2'

This Query will return record with its duplicate count,how many times duplication occurred.