I need to show the three closest hospitals to every house. I have a table of properties and a table of hospitals. Both are in MSSQL2012, have a geography column that is indexed and are in the same 4326 SRID.
I cant find any examples on how to perform ST_Distance across different tables, returning the top 3.
As per Evans suggestion i have tried the following but the query is not valid
SELECT *
FROM (
SELECT * , row_number OVER (
PARTITION BY [dbo].[houses].[geogtab]
ORDER BY [dbo].[houses].[geogtab].STDistance([dbo].[hospitals].[geogtab]) ASC
) AS i
FROM [dbo].[houses]
JOIN [dbo].[hospitals]
ON [dbo].[houses].[geogtab].STBuffer(100000).STIntersects([dbo].[hospitals].[geogtab])
ORDER BY [dbo].[houses].[geogtab].STDistance([dbo].[hospitals].[geogtab] ) ASC
) AS t
WHERE t.i <= 3;
I am unsure if this is formatting related and am unsure from reading this if the distance will be an output in the result table.