php - 如何使用几个查询从多个表中计算出东西?

标签 php sql postgresql postgis

注意:本题与PostGIS和Postgresql相关,用PHP实现

现在我有了表 A:

gid | kstart  | kend    | ctrl_sec_no | the_geom | 
626 | 238     | 239     | 120802      | 123456   |
638 | 249     | 250     | 120802      | 234567   |
4037| 239     | 249     | 120802      | 345678   |

注意:the_geom 是一个几何值(TYPE:LINE),在这种情况下,我将它们随机化以提高可读性

和表 B:

gid | ctrl_sec_no | x   | the_geom
543 | 120802      | 239 | null
544 | 120802      | 247 | null

[PostGIS 描述] 这两个表通过ctrl_sec_no关联,这意味着来自表A的ctrl_sec_no 120802上的3条连续LINE连接成一条线并包含来自表B的两个POINT。我们只知道距离{MAX(kend) - MIN(kstart)} LINE 和它在 LINE 上的公里数 (x)。

问题是 PostgreSQL 的查询是什么..

(a.) 从 A.kend 中选择最高值,减去 A.kstart 中的最低值 -> 250 - 238 = 12

(b.) 从 A.kend 中选择最大值,减去 B 中的 'x' 值 -> 250 - 239 = 11

(c.) 从这两个值计算比率 ((b.)/(a.)) -> 11/12

(d.) 使用 PostGIS:ST_Interpolate -> ST_Interpolate(A.the_geom, 11/12) 注意:此函数用于连同LINE一起查找POINT,另一方面定义POINT所在的位置

(e.) 我们将从 (d.) 中获取一个值,并使用它来更新表 B 中的“the_geom”列,该列最初为 NULL。

(f.) 对表 B 中的每一行循环这组查询。

[PostGIS 说明] 这组查询的目的是通过计算一些数学来确定表 B 中的 the_geom,并将输出放入 ST_Interpolate 函数以获得表 B 中的 POINT 所在的 the_geom。

感谢 Advanced,我知道这是一个非常复杂的问题。我不介意您是否会使用太多查询。只是为了得到正确的值。

这些是在 danihp 的帮助下的实际查询(最终)。

with CTE( max_kend) as (
 SELECT MAX(A.kend)
 FROM centerline A
),
r_b as (
 select B.ctrl_sec_no,B.gid, MAX(CTE.max_kend) - B.km as b
  FROM land_inventory B cross join CTE group by B.gid,B.ctrl_sec_no,B.km
),
r_a as (
    SELECT MAX(A.kend) - MIN(A.kstart) as a
    FROM centerline A
),
r_ratio as (
  select r_b.gid, r_b.b / r_a.a  as my_ratio
  from r_a cross join r_b
),
r_new_int as (
select B.gid,r_ratio.my_ratio,B.ctrl_sec_no,B.km,ST_AsText(ST_Envelope(ST_Collect(ST_line_interpolate_point(A.the_geom,  r_ratio.my_ratio )))) as new_int from centerline A, land_inventory B inner join r_ratio on B.gid = r_ratio.gid where A.ctrl_sec_no = B.ctrl_sec_no group by B.ctrl_sec_no,B.gid,r_ratio.my_ratio,B.km order by B.ctrl_sec_no
) 
UPDATE land_inventory
set land_inventory.the_geom = n.new_int
from r_new_int n
where 
  n.gid = land_inventory.gid and
  land_inventory.the_geom is NULL;

最佳答案

好吧,我们走吧。

(一)

SELECT MAX(A.kend) - MIN( A.kstart) as a
FROM Table A

(b)

EDITED 假设 gid 是表 B 的 PK ...

with CTE( max_kend, min_x) as (
 SELECT MAX(A.kend), NULL
 FROM TableA A
)
select B.gid, MAX(CTE.max_kend) - B.min_x as b
 FROM TableB B 
 cross join CTE

(三)

with CTE( max_kend, min_x) as (
 SELECT MAX(A.kend), NULL
 FROM TableA A
),
r_b as (
 select B.gid, MAX(CTE.max_kend) - B.min_x as b
  FROM TableB B 
  cross join CTE
),
r_a as (
    SELECT MAX(A.kend) - MIN( A.kstart) as a
    FROM Table A
)
select r_b.gid, r_a.a / r_b.b as my_ratio
from r_a cross join r_b

(d)

with CTE( max_kend, min_x) as (
 SELECT MAX(A.kend), NULL
 FROM TableA A
),
r_b as (
 select B.gid, MAX(CTE.max_kend) - B.min_x as b
  FROM TableB B 
  cross join CTE
),
r_a as (
    SELECT MAX(A.kend) - MIN( A.kstart) as a
    FROM Table A
),
r_ratio as (
  select r_b.gid, r_a.a / r_b.b as my_ratio
  from r_a cross join r_b
)
select ST_Interpolate(A.the_geom,  r_ratio.my_ratio )   
from TableB B 
inner join r_ratio on B.gid = r_ratio.gid

(e, f)

with CTE( max_kend, min_x) as (
 SELECT MAX(A.kend), NULL
 FROM TableA A
),
r_b as (
 select B.gid, MAX(CTE.max_kend) - B.min_x as b
  FROM TableB B 
  cross join CTE
),
r_a as (
    SELECT MAX(A.kend) - MIN( A.kstart) as a
    FROM Table A
),
r_ratio as (
  select r_b.gid, r_a.a / r_b.b as my_ratio
  from r_a cross join r_b
),
r_new_int as (
  select ST_Interpolate(A.the_geom,  r_ratio.my_ratio ) as new_int
  from TableB B 
  inner join r_ratio on B.gid = r_ratio.gid
)
UPDATE tableB
set tableB.the_geom = n.new_int
from r_new_int n
where 
  n.gid = tableB.gid and
  tableB.the_geom  is NULL

免责声明,并非测试。

已编辑

with CTE( max_kend) as (
 SELECT MAX(A.kend)
 FROM centerline A
),
r_b as (
 select B.ctrl_sec_no,B.gid, MAX(CTE.max_kend) - B.km as b
  FROM land_inventory B cross join CTE group by B.gid,B.ctrl_sec_no,B.km
),
r_a as (
    SELECT MAX(A.kend) - MIN(A.kstart) as a
    FROM centerline A
),
r_ratio as (
  select r_b.gid, r_b.b / r_a.a  as my_ratio
  from r_a cross join r_b
),
r_new_int as (
  select     
    B.gid,
    r_ratio.my_ratio,
    B.ctrl_sec_no,B.km,
    ST_AsText(ST_Envelope(ST_Collect(
          ST_line_interpolate_point(A.the_geom,  r_ratio.my_ratio 
    )))) as new_int 
  from 
    centerline A inner join
    land_inventory B 
       on A.ctrl_sec_no = B.ctrl_sec_no 
       inner join 
    r_ratio on B.gid = r_ratio.gid 
  group by B.ctrl_sec_no,B.gid,r_ratio.my_ratio,B.km order by B.ctrl_sec_no
) 
UPDATE land_inventory
set the_geom = n.new_int
from r_new_int n
where 
  n.gid = land_inventory.gid and
  land_inventory.the_geom is NULL;

关于php - 如何使用几个查询从多个表中计算出东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9211362/

相关文章:

php - Codeigniter - View 不违反 DRY 的最佳实践

php - 从 PHP 数组创建 Javascript 数组

postgresql - 我怎样才能每 15 分钟平均一次这些数据?

mysql - 在 postgresql 的 WHERE 子查询中使用条件

django - Postgresql 即使有索引也慢连接?

php - 对数组进行排序并将索引存储在单独的数组中

php - mysql中的双重插入

SQL Server - 分区表扫描整个表而不是给定范围

SQL Select 语句在两个列表之间

mysql - 如何根据关联表确定计数?