In the following query, why is it that we have to limit the results returned from each Partition by using the clause WHERE foo.row_num < 3 outside of the subquery foo but not from within the subquery with WHERE row_num < 3?
Query
SELECT pid, land_type, row_num, road_name, round(CAST(dist_km AS numeric), 2) AS dist_km
FROM (
SELECT ROW_NUMBER()
OVER (
PARTITION by loc.pid
ORDER BY ST_Distance(r.the_geom, loc.the_geom)
) as row_num,
loc.pid, loc.land_type, r.road_name,
ST_Distance(r.the_geom, loc.the_geom)/1000 as dist_km
FROM ch05.land AS loc
LEFT JOIN ch05.road AS r
ON ST_DWithin(r.the_geom, loc.the_geom, 1000)
WHERE loc.land_type = 'police station'
) AS foo
WHERE foo.row_num < 3
ORDER BY pid, row_num;
Query that does not work
SELECT pid, land_type, row_num, road_name, round(CAST(dist_km AS numeric), 2) AS dist_km
FROM (
SELECT ROW_NUMBER()
OVER (
PARTITION by loc.pid
ORDER BY ST_Distance(r.the_geom, loc.the_geom)
) as row_num,
loc.pid, loc.land_type, r.road_name,
ST_Distance(r.the_geom, loc.the_geom)/1000 as dist_km
FROM ch05.land AS loc
LEFT JOIN ch05.road AS r
ON ST_DWithin(r.the_geom, loc.the_geom, 1000)
WHERE loc.land_type = 'police station'
AND row_num < 3
) AS foo
ORDER BY pid, row_num;
Error: ERROR: column "row_num" does not exist