I've got the following task to solve:
I've got two tables; one contains buildings (houses) as points and the other contains train-stations (at_bhf_classified) as points. I need to find out the distance from each house to the next trainstation. Well, this is more or less easy. But now the issue: The trainstation are categorized. That means: Give the distance to the next station of category 1 if within a 25km radius. If no category 1 station is found within this distance, give me the distance to cat 2 station within the 25 km radius.
So far I use the following statement to calculate the nearest category 1 station:
WITH dist_min
AS (SELECT h.h_key,
Min(St_distance_sphere(h.geom, b.geom)) dist_min
FROM house h,
at_bhf_classified b
WHERE St_distance_sphere(h.geom, b.geom) < 25000 and b.kat=1
GROUP BY h.h_key)
SELECT h.h_key,
b.bhfname,
b.kat,
b.bhfnummer,
round(cast(d.dist_min as numeric),2)
FROM dist_min d,
house h,
at_bhf_classified b
WHERE St_distance_sphere(h.geom, b.geom) = dist_min
AND St_distance_sphere(h.geom, b.geom) < 25000
GROUP BY h.h_key, b.bhfname, b.kat, b.bhfnummer, d.dist_min
ORDER BY h.h_key
Do you know how to include the second condition explained further up?
Thanks for you help! Carsten