Sql 计算边界框内的点/坐标

Sql 计算边界框内的点/坐标,sql,postgresql,pgadmin,Sql,Postgresql,Pgadmin,我有两张桌子。第一个表包含以下列:起始经度、起始经度、结束经度、结束经度、总和。sum列为空,需要根据第二个表进行填充 第二个表包含3列:点纬度、点经度 表1 ------------------------- |45 | 50 | 46 | 51 | null| ----|--------------------- |45 | 54 | 46 | 57 | null| -------------------------- --------------- | 45.5 | 55.2 | ---

我有两张桌子。第一个表包含以下列:起始经度、起始经度、结束经度、结束经度、总和。sum列为空,需要根据第二个表进行填充

第二个表包含3列:点纬度、点经度

表1

-------------------------
|45 | 50 | 46 | 51 | null|
----|---------------------
|45 | 54 | 46 | 57 | null|
--------------------------
---------------
| 45.5 | 55.2 |
---------------
| 45.8 | 50.6 |
---------------
| 45.2 | 56   |
---------------
表2:

-------------------------
|45 | 50 | 46 | 51 | null|
----|---------------------
|45 | 54 | 46 | 57 | null|
--------------------------
---------------
| 45.5 | 55.2 |
---------------
| 45.8 | 50.6 |
---------------
| 45.2 | 56   |
---------------
table1-row1中的空值为1,而row2中的空值为2。它是位于边界框内的点数

我可以在python中编写函数来读取数据帧之间的值。如何在Postgresql中实现这一点。这是我针对自己的情况提出的问题陈述示例。

更新 此版本在PostgreSql 9.3上使用SQL FIDLE进行了测试

UPDATE table1 a
SET sum = sub.point_count
FROM (SELECT a.start_lat, a.end_lat, a.start_lon, a.end_lon, COUNT(*) as point_count
      FROM table1 a, table2 b
      WHERE b.point_lat BETWEEN start_lat AND a.end_lat
        AND b.point_lon BETWEEN a.start_lon AND a.end_lon
      GROUP BY a.start_lat, a.end_lat, a.start_lon, a.end_lon) as sub
WHERE a.start_lat = sub.start_lat
  AND a.end_lat = sub.end_lat
  AND a.start_lon = sub.start_lon
  AND a.end_lon = sub.end_lon;

原始答案

这是我的解决方案,它是在MySQL上测试的,但是没有关于这段代码的具体内容,所以它也应该在PostgreSql上工作

UPDATE table1 a,
  (SELECT a.start_lat, a.end_lat, a.start_lon, a.end_lon, COUNT(*) as count
   FROM table1 a, table2 b
   WHERE b.point_lat BETWEEN start_lat AND a.end_lat
   AND b.point_lon BETWEEN a.start_lon AND a.end_lon
   GROUP BY a.start_lat, a.end_lat, a.start_lon, a.end_lon) as sub
SET sum = count
WHERE a.start_lat = sub.start_lat
  AND a.end_lat = sub.end_lat
  AND a.start_lon = sub.start_lon
  AND a.end_lon = sub.end_lon 
请注意,如果表1包含PK Id列,则此查询将短得多。

Update 此版本在PostgreSql 9.3上使用SQL FIDLE进行了测试

UPDATE table1 a
SET sum = sub.point_count
FROM (SELECT a.start_lat, a.end_lat, a.start_lon, a.end_lon, COUNT(*) as point_count
      FROM table1 a, table2 b
      WHERE b.point_lat BETWEEN start_lat AND a.end_lat
        AND b.point_lon BETWEEN a.start_lon AND a.end_lon
      GROUP BY a.start_lat, a.end_lat, a.start_lon, a.end_lon) as sub
WHERE a.start_lat = sub.start_lat
  AND a.end_lat = sub.end_lat
  AND a.start_lon = sub.start_lon
  AND a.end_lon = sub.end_lon;

原始答案

这是我的解决方案,它是在MySQL上测试的,但是没有关于这段代码的具体内容,所以它也应该在PostgreSql上工作

UPDATE table1 a,
  (SELECT a.start_lat, a.end_lat, a.start_lon, a.end_lon, COUNT(*) as count
   FROM table1 a, table2 b
   WHERE b.point_lat BETWEEN start_lat AND a.end_lat
   AND b.point_lon BETWEEN a.start_lon AND a.end_lon
   GROUP BY a.start_lat, a.end_lat, a.start_lon, a.end_lon) as sub
SET sum = count
WHERE a.start_lat = sub.start_lat
  AND a.end_lat = sub.end_lat
  AND a.start_lon = sub.start_lon
  AND a.end_lon = sub.end_lon 

请注意,如果表1包含PK Id列,则此查询会短得多。

表中没有Id列吗?表中没有Id列吗?