Places Table
- PlaceId PK
- Name
- GeoPoint (GeoPoint type)
- etc…
PlaceCats Table (the join table)
- PlaceId PK FK
- CatId PK FK
PlaceCategories Table
- CatId PK
- Name
- etc…
Here’s my query, to return places, with entries in the PlaceCats table (by a series of catIds), and within 25km of a Lat/Lng value.
SELECT Places.*
FROM Places
INNER JOIN PlaceCats
ON Places.PlaceId = PlaceCats.PlaceId
WHERE PlaceCats.CatId IN (2,3,4,5)
AND Places.GeoPoint.STDistance(geography::Point(35.75094975999387, 139.39780220389366, 4326)) < ((25 * 1000))
ORDER BY Places.Name ASC
It’s returning places, but I need to add DISTINCT after the SELECT… apparently you can’t use DISTINCT when selecting geography data. I need to make sure I’m not getting the same place multiple times (as they can have multiple entries in the join table).
Update
When I insert a place into the Places table, the GeoPoint is set like so:
SqlGeography.Point(sentPlace.Lat.Value, sentPlace.Lng.Value, 4326);
It look to me like you have your long/lats the wrong way round.
As for selecting DISTINCT geographies, you can perform the DISTINCT operation over the WKT (Well Known Text) in a nested query, see below:
Your query would look something like this:
One further tip, assuming you have a spatial index on your table, it speeds spatial queries up no end by using an index hit: