Postgresql / PostGIS,在计算列上排序时计算运行总计直到一定量

时间:2013-09-04 08:39:09

标签: sql postgresql select postgis

我的查询与this question非常相似,我正在创建一个运行总计,直到达到某个值。在我的情况下,我计算县的人口,直到我达到理想的人口。

我想按照到lng / lat的距离来命令搜索,但是,我要添加最接近的县。我在下面运行此查询,但没有结果。当我改为ORDER BY row_id或任何非空间计算的列时,它会返回结果。我也尝试使用两个WITH条款,但我仍然没有得到任何结果。我已经尝试将distance转换为整数,因为它可能没有正确输入但是没有帮助。

我已经设置了一个测试服务器,您可以使用此端点运行查询 http://sqltestmcr.cartodb.com/api/v2/sql?q=<your query statement here>

这是我正在尝试的查询。以下是可行的变体。 您可以使用以下链接查看此查询的结果: 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
    *
    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 <= 1400

以下两个查询适用于我设置ORDER BY row_idORDER BY row_id_2的位置 使用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
    *
    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 <= 1400

使用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

1 个答案:

答案 0 :(得分:1)

一切正常,只是最接近查询点的县人口为1700,因此超过了总的截止时间。

将运行总截止值提高到5000(query)会选择两行:

{'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}

同样,如果您有一个不同于最近县的查询点,则&lt; 1400,那么应该返回1行或更多行。