sql - Postgresql / PostGIS, calculating a running total until a certain amount while ordering on calculated column -


i have query similar this question i'm creating running total until reach value. in case, i'm counting population of counties until reach desired population.

i want order search distance lng/lat, however, add counties closest first. i'm running query below no results. when instead order row_id or non-spatially-calculated column, comes results. i've tried using 2 with clauses still no results. i've tried casting distance integer since maybe wasn't typed didn't help.

i've set test server can run queries endpoint http://sqltestmcr.cartodb.com/api/v2/sql?q=<your query statement here>

this query trying. below variations work. can see results of query link: http://sqltestmcr.cartodb.com/api/v2/sql?q=select%20*%20from%20(%20select%20county,%20the_geom,%20distance,%20row_id_2,%20sum(population)%20over%20(order%20by%20distance%20asc)%20as%20running_total%20from%20(%20select%20row_id,%20county,%20population,%20the_geom,%20row_id%20*%202%20as%20row_id_2,%20st_distance(%20st_centroid(the_geom),%20st_geomfromtext('point(-72.1235%2042.3521)',%204326)%20)%20as%20distance%20from%20counties_ny_export)%20sq1)%20sq2%20where%20running_total%20%3c=%201400

    select     *     (         select          county,         the_geom,         distance,         row_id_2,         sum(population) on (order distance asc) running_total       (         select          row_id,         county,          population,         the_geom,         row_id * 2 row_id_2,         st_distance(           st_centroid(the_geom),            st_geomfromtext('point(-72.1235 42.3521)', 4326)         ) distance                  counties_ny_export     ) sq1     ) sq2        running_total <= 1400 

the following 2 queries works set order row_id or order row_id_2 test link row_id: http://sqltestmcr.cartodb.com/api/v2/sql?q=select%20*%20from%20(%20select%20county,%20the_geom,%20distance,%20row_id_2,%20sum(population)%20over%20(order%20by%20row_id%20asc)%20as%20running_total%20from%20(%20select%20row_id,%20county,%20population,%20the_geom,%20row_id%20*%202%20as%20row_id_2,%20st_distance(%20st_centroid(the_geom),%20st_geomfromtext('point(-72.1235%2042.3521)',%204326)%20)%20as%20distance%20from%20counties_ny_export)%20sq1)%20sq2%20where%20running_total%20%3c=%201400

    select     *     (         select          county,         the_geom,         distance,         row_id_2,         sum(population) on (order row_id asc) running_total       (         select          row_id,         county,          population,         the_geom,         row_id * 2 row_id_2,         st_distance(           st_centroid(the_geom),            st_geomfromtext('point(-72.1235 42.3521)', 4326)         ) distance                  counties_ny_export     ) sq1     ) sq2        running_total <= 1400 

test link row_id_2: http://sqltestmcr.cartodb.com/api/v2/sql?q=select%20*%20from%20(%20select%20county,%20the_geom,%20distance,%20row_id_2,%20sum(population)%20over%20(order%20by%20row_id_2%20asc)%20as%20running_total%20from%20(%20select%20row_id,%20county,%20population,%20the_geom,%20row_id%20*%202%20as%20row_id_2,%20st_distance(%20st_centroid(the_geom),%20st_geomfromtext('point(-72.1235%2042.3521)',%204326)%20)%20as%20distance%20from%20counties_ny_export)%20sq1)%20sq2%20where%20running_total%20%3c=%201400

everything working fine, happens closest county query point has population of 1700 , exceeds running total cut-off.

raising running total cut-off 5000 (query) selects 2 rows:

{'fields': {'county': {'type': 'string'},             'distance': {'type': 'number'},             'population': {'type': 'number'},             'row_id_2': {'type': 'number'},             'running_total': {'type': 'number'}},  'rows': [{'county': 'rensselaer',            'distance': 1.4299124873995,            'population': 1700,            'row_id_2': 34,            'running_total': 1700},           {'county': 'columbia',            'distance': 1.51173290729954,            'population': 2200,            'row_id_2': 44,            'running_total': 3900}],  'time': 0.002,  'total_rows': 2} 

similarly, if have different query point closest county < 1400, there should 1 or more rows returned.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -