문제

I have a SQL Server 2012 location table, which has a column geo of type geography

I have a user table which links to the location table

location

locationId int
geo geography

user

userId int
locationId int

I will know the id of the location and I know how to do a distance query with stdistance. But whats the best way of comparing the two distances in a query. I can do it with a subselect but is there a better way

The sub select looks something like

select 
   suburb, state, postcode, 
   geo.STDistance((select geo from location where locationId = 47))/1000 kms
from location
order by kms desc
도움이 되었습니까?

해결책

Is location #47 special somehow? Will it always be 47? Either way, you can stick it in a variable

declare @HQ geography;
select @HQ = geo from location where locationid = 47;

select 
   suburb, state, postcode, 
   geo.STDistance(@HQ)/1000 kms
from location
order by kms desc

If you (for whatever reason) want it all in one query, you could try an outer apply

select 
   suburb, state, postcode, 
   geo.STDistance(HQ.geo)/1000 kms
from location
outer apply (select geo from location where locationid = 47) as HQ
order by kms desc
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top