Postgresql/PostGIS,在计算列上排序时,计算运行总数,直到达到一定数量
我有一个查询,非常类似于在达到某个值之前创建一个运行总数的查询。在我的例子中,我正在计算各县的人口,直到达到所需的人口 但是,我想按距离lng/lat的距离排序搜索,以便首先添加距离最近的县。我正在下面运行此查询,但未得到任何结果。当我改为按行id排序或任何非空间计算的列时,它会返回结果。我也尝试过使用两个带有子句的Postgresql/PostGIS,在计算列上排序时,计算运行总数,直到达到一定数量,sql,postgresql,select,postgis,Sql,Postgresql,Select,Postgis,我有一个查询,非常类似于在达到某个值之前创建一个运行总数的查询。在我的例子中,我正在计算各县的人口,直到达到所需的人口 但是,我想按距离lng/lat的距离排序搜索,以便首先添加距离最近的县。我正在下面运行此查询,但未得到任何结果。当我改为按行id排序或任何非空间计算的列时,它会返回结果。我也尝试过使用两个带有子句的,但仍然没有结果。我尝试将distance转换为整数,因为它可能没有正确键入,但没有帮助 我已经设置了一个测试服务器,您可以在其中使用该端点运行查询 http://sqltestmc
,但仍然没有结果。我尝试将distance
转换为整数,因为它可能没有正确键入,但没有帮助
我已经设置了一个测试服务器,您可以在其中使用该端点运行查询
http://sqltestmcr.cartodb.com/api/v2/sql?q=
这就是我正在尝试的问题。下面是一些有效的变体。
您可以通过以下链接查看此查询的结果:
http://sqltestmcr.cartodb.com/api/v2/sql?q=SELECT%20*%20FROM%20(%20SELECT%20Country、%20the_geom、%20distance、%20row_id_2、%20um(人口)%20FROM%20(订单%20by%20distance%20asc)%20as%20running_total%20FROM%20(%20SELECT%20row_id、%20Country、%20人口、%20the_geom、%20row_id%20*%202%20as%20row_id_2、%20Stum距离(%20Stud_质心(地理),%20ST\u geomefromtext('POINT(-72.1235%2042.3521)“%204326)%20)%20AS%20distance%20FROM%20countries\u ny\u export)%20sq1)%20sq2%20其中%20running\u总计%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
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
带有行id\u 2的测试链接
:
http://sqltestmcr.cartodb.com/api/v2/sql?q=SELECT%20*%20FROM%20(%20SELECT%20county、%20the_geom、%20distance、%20row_id_2、%20um(人口)%20FROM%20(订单%20by%20row_id_2%20asc)%20as%20running_total%20FROM%20(%20SELECT%20row_id、%20Country、%20People、%20the_geom、%20row_id%20*%20*%202%20as%20row_id_质心(%20ST geom),%20ST\u GeomFromText('POINT(-72.1235%2042.3521)“%204326)%20)%20AS%20distance%20FROM%20countries\u ny\u export)%20sq1)%20sq2%20其中%20running\u total%20%3C=%201400
一切正常,恰好距离查询点最近的县人口为1700,因此超过了运行总截止值
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
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
将运行总截止值提高到5000()将选择两行:
{'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}
类似地,如果您有一个与小于1400的县最近的不同查询点,则应返回1行或多行。如果您可以提供一些示例,则解决问题会容易得多SQL Fiddle没有PostGIS功能,而且PostGIS似乎是一个问题,非空间计算的列值有效。相反,我创建了一个测试数据库,您可以通过上面的端点进行查询。您可以使用http://sqltestmcr.cartodb.com/api/v2/sql?q=SELECT *从纽约州出口
。查询不需要URI编码。