Note that there are some explanatory texts on larger screens.

plurals
  1. POPostgresql / PostGIS, calculating a running total until a certain amount while ordering on calculated column
    primarykey
    data
    text
    <p>I have a query very similar to <a href="https://stackoverflow.com/questions/10549327/postgresql-select-until-certain-total-amount-is-reached" title="this one">this question</a> where I'm creating a running total until I reach a certain value. In my case, I'm counting population of counties until I reach the desired population. </p> <p>I want to order the search by the distance to a lng/lat, however, so that I add counties that are closest first. I'm running this query below but I get no results. When I instead do <code>ORDER BY row_id</code> or any non-SPATIALLY-calculated column, it comes back with results. I've also tried it using two <code>WITH</code> clauses but I still get no results. I've tried casting <code>distance</code> as integer since maybe it wasn't typed properly but that didn't help.</p> <p>I've set up a test server where you can run queries with this endpoint <code>http://sqltestmcr.cartodb.com/api/v2/sql?q=&lt;your query statement here&gt;</code></p> <p>This is the query that I am trying. Below are variations that work. You can see the results of this query with this link: <code>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</code></p> <pre><code> SELECT * FROM ( SELECT county, the_geom, distance, row_id_2, sum(population) over (order by distance asc) as running_total FROM ( SELECT row_id, county, population, the_geom, row_id * 2 AS row_id_2, ST_Distance( ST_Centroid(the_geom), ST_GeomFromText('POINT(-72.1235 42.3521)', 4326) ) AS distance FROM counties_ny_export ) sq1 ) sq2 where running_total &lt;= 1400 </code></pre> <p>The following two queries works where I set <code>ORDER BY row_id</code> or <code>ORDER BY row_id_2</code> Test link with <code>row_id</code>: <code>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</code></p> <pre><code> SELECT * FROM ( SELECT county, the_geom, distance, row_id_2, sum(population) over (order by row_id asc) as running_total FROM ( SELECT row_id, county, population, the_geom, row_id * 2 AS row_id_2, ST_Distance( ST_Centroid(the_geom), ST_GeomFromText('POINT(-72.1235 42.3521)', 4326) ) AS distance FROM counties_ny_export ) sq1 ) sq2 where running_total &lt;= 1400 </code></pre> <p>Test link with <code>row_id_2</code>: <code>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</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload