Select cities with the same name in different states

Asked

Viewed 345 times

3

I have the table (result of a query) with the cities and their respective state...

inserir a descrição da imagem aqui

... now only display cities with equal names and different states:

inserir a descrição da imagem aqui

How do I command?

1 answer

3


It would be something like this, first you take only the ones that have more than one recurrence in the table, and then you do a query by name, as you did not know the name of your table just replace the tag [sua_tabela] by table name and test:

SELECT
    *
FROM [sua_tabela]
WHERE cidade = (
SELECT
    cidade
FROM 
(SELECT 
    COUNT(*) AS qtd,
    cidade
from [sua_tabela]
GROUP BY cidade
HAVING qtd > 1) as aux);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.