I understand how GROUP BY works and I also understand why my query does not bring the results I am expecting. However, what would be the best way to eliminate duplicates in this case?
Let's say we have the following tables:
City
Id Name
---------------------
1 Seattle
2 Los Angeles
3 San Francisco
Person
Id Name CityId
----------------------------
1 John Smith 1
2 Peter Taylor 1
3 Kate Elliot 1
4 Bruno Davis 2
5 Jack Brown 2
6 Bob Stewart 2
7 Tom Walker 3
8 Andrew Garcia 3
9 Kate Bauer 3
I want to retrieve a list of all cities and just one person that lives in each city.
Using GROUP BY:
SELECT c.Id, c.Name as PersonName, p.Name as CityName
FROM City c
INNER JOIN Person p ON p.CityId = c.Id
GROUP BY c.Name, p.Name
Result:
Id PersonName CityName
----------------------------
1 John Smith Seattle
1 Peter Taylor Seattle
1 Kate Elliot Seattle
2 Bruno Davis Los Angeles
2 Jack Brown Los Angeles
2 Bob Stewart Los Angeles
3 Tom Walker San Francisco
3 Andrew Garcia San Francisco
3 Kate Bauer San Francisco
Using DISTINCT:
SELECT DISTINCT c.Id, c.Name as PersonName, p.Name as CityName
FROM City c
INNER JOIN Person p ON p.CityId = c.Id
Same result.
Just to be very clear. This is the expected result:
Id PersonName CityName
----------------------------
1 John Smith Seattle
2 Bruno Davis Los Angeles
3 Tom Walker San Francisco
Would subquery be the only solution for this case?