Consider the following poorly organized table of location data for cities. This table, which has no primary key, is very similar to a data set that I had to work with recently on a project. The problem is that I had to display the city names on a map and, since the location data varies slightly for some cities, I originally had a cluster of overlapping names instead of a single clearly-visible city name. Needless to say, this was not acceptable and, quite frankly, looked ridiculous.
City Longitude Latitude
...
Boston, MA -71.0 42.4
Boston, MA -71.03 42.37
...
Washington, DC -77.045 38.855
Washington, DC -77.045 38.8455
Washington, DC -77.04 38.855
...
To solve this problem, I eventually came up with the following SQL query, which just arbitrarily picks one from each series of cities with matching names:
SELECT * FROM Cities
WHERE rowid IN (
SELECT MIN(rowid) FROM Cities
GROUP BY City
)
This solved the problem admirably!
Note that this query probably only works in Oracle, but should be adaptable to other DBMSs by substituting the appropriate 'rowid' construct.
No comments:
Post a Comment