【问题标题】:Postgresql / PostGIS, calculating a running total until a certain amount while ordering on calculated columnPostgresql / PostGIS,在计算列上排序时计算运行总计直到一定数量
【发布时间】:2013-09-04 08:39:08
【问题描述】:

我有一个与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_idhttp://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

【问题讨论】:

  • 如果您可以在SQLFiddel 上提供一些示例,那么解决您的问题会容易得多
  • SQL Fiddle 没有 PostGIS 功能,并且 PostGIS 似乎是问题,因为其他非空间计算的列值有效。相反,我创建了一个测试数据库,您可以通过上面的端点进行查询。您可以使用http://sqltestmcr.cartodb.com/api/v2/sql?q=SELECT * FROM counties_ny_export 查看完整数据。查询不必是 URI 编码的。

标签: sql postgresql select postgis


【解决方案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}

类似地,如果您有一个不同的查询点最接近具有

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-30
    • 2017-10-04
    • 2011-08-07
    • 2021-12-20
    • 2016-11-14
    • 2023-03-23
    • 1970-01-01
    • 2018-01-12
    相关资源
    最近更新 更多