| name | year | latitude | longitude |
|--------------|------|----------|-----------|
| Cleveland | 1800 | 10 | 11 |
| Cleveland | 1810 | 10 | 11 |
| Medina | 1811 | 12 | 13 |
| Dayton | 1812 | 14 | 15 |
| Sandusky | 1105 | 50 | 50 |
| Mount Vernon | 1813 | 50 | 50 |
I want to select each unique combinations of latitude and longitude. So I want to filter out any duplicate pairs. I also need to filter out any records whose year is less than 1500.
This is the subset I'm trying to achieve:
| name | year | latitude | longitude |
|--------------|------|----------|-----------|
| Cleveland | 1800 | 10 | 11 |
| Medina | 1811 | 12 | 13 |
| Dayton | 1812 | 14 | 15 |
| Mount Vernon | 1813 | 50 | 50 |
Each records year is greater than 1500 and there aren't any duplicate lat,long pairs.
I've tried to find a way to use DISTINCT. Nothing I've found has worked.
I also have tried using GROUP BY:
SELECT *
FROM users
GROUP BY latitude, longitude
HAVING year > 1500;
The issue with the above query is that is eliminates both of the following records which contain the lat,long pair of 50,50:
| name | year | latitude | longitude |
|--------------|------|----------|-----------|
| Sandusky | 1105 | 50 | 50 |
| Mount Vernon | 1813 | 50 | 50 |
The group is eliminated because Sandusky's year is less than 1500. I don't want Sandusky's record, but I do want Mount Vernon.
I noticed that if if the two records where switched like so:
| name | year | latitude | longitude |
|--------------|------|----------|-----------|
| Mount Vernon | 1813 | 50 | 50 |
| Sandusky | 1105 | 50 | 50 |
...then the group's year is set as 1813 and the group is not eliminated. I thought maybe sorting by year would fix it, but it didn't:
SELECT *
FROM users
GROUP BY latitude, longitude
HAVING year > 1500
ORDER BY year DESC;
Is what I'm attempting possible?
How about this?
SELECT `id`, `name`, MAX(users.year) as `year`, latitude, longitude
FROM users
WHERE year > 1500
GROUP BY latitude, longitude;
Results in:
| 7 | Columbus | 1978 | 7 | 8
| 1 | Cleveland | 1800 | 10 | 11
| 3 | Medina | 1811 | 12 | 13
| 4 | Dayton | 1812 | 14 | 15
| 6 | Mount Vernon | 1813 | 50 | 50
The only difference is where the WHERE/HAVING is, because it is before the GROUP BY statement, it will do the filtering BEFORE the grouping happens and thus you get the desired result.
The MAX(users.year)ensure that you always get the largest year on the set. If this doesn't matter to you, you can replace SELECT `id`, `name`, MAX(users.year) as `year`, latitude, longitude with SELECT *
Maybe I didn't understand the problem, but it would be this simple:
select * from users u where u.year > 1500;
I don't know what you want to do in case there are more than one pair of the same coordinates with a year greater than 1500.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With