sql - 如何对单独的查询集执行数学公式

标签 sql postgresql

我们有一个获取 GPS 坐标数据的程序。我们可以根据数据库中的区域 ID 获取数据集,它看起来像这样:

gps_coords | year      | value
105        | 2010      |  5.63
102        | 1990      |  3.2
103        | 2000      |  13.23
...

现在,我们想将它和另一个查询集结合起来,使用像 a.value + b.value(a.value+50)*b.value/这样的 sql 100。我们还按指标(他们想要查询的数据集)过滤我们的查询。

问题是如何将两个 gps_coords 作为一列检索。我认为我们必须在同一张表上执行 JOIN,但我不知道如何在同一列中同时获取 a.gps_coords 和 b.gps_coords。

我的查询(下图)执行时间为 100 毫秒,行数为零。所以,我不确定出了什么问题。有谁知道如何在同一列中同时获得 a 和 b 的 gps_coords?我正在使用 Postgresql,但任何事情都会有所帮助。谢谢!

架构

数据:

gps_coords
year
value
metric

地区:

gps_coords
region_id

示例数据:

数据

|  gps_coords  |  year  |  value  |  metric  |
|  506         |  2010  |  23.23  |  5       |
|  507         |  2010  |  10.32  |  5       |
|  508         |  2010  |  28.5   |  5       |
|  509         |  2010  |  45.24  |  5       |
|  506         |  2010  |  213.53 |  4       |
|  507         |  2010  |  0      |  4       |
|  508         |  2010  |  434.4  |  4       |
|  509         |  2010  |  381.1  |  4       |

地区

|  gps_coords  |  region_id  |
|  506         |  1          |
|  506         |  2          |
|  506         |  3          |
|  507         |  1          |
|  508         |  1          |
|  508         |  3          |
|  509         |  1          |
|  509         |  2          |

期望的输出:

假设我想要区域 1 中的度量 5 的坐标,添加区域 3 中的度量 4(与 gps_coords 506 重叠),我想返回所有 gps_coords(无论区域如何),然后返回指定的值(添加到哪里它们相交):

|  gps_coords  |  year  |  value  |
|  506         |  2010  |  233.76 |
|  507         |  2010  |  0      |
|  508         |  2010  |  434.4  |
|  509         |  2010  |  45.24  |

示例(不正确的)SQL:

SELECT DISTINCT init.gps_coords, init.year, a.value + b.value as value

FROM data as init

INNER JOIN data as a USING (metric, value)
INNER JOIN data as b USING (metric, value)

INNER JOIN regions as r
ON (init.gps_coords = r.gps_coords)
AND r.region_id = 1

INNER JOIN regions as ra
ON (a.gps_coords = ra.gps_coords)
AND ra.region_id = 2

INNER JOIN regions as rb
ON (init.gps_coords = rb.gps_coords)
AND rb.region_id = 3

WHERE a.metric = 5
AND b.metric = 4
ORDER BY init.gps_coords

上面是每个区域(区域 1)的所有坐标,然后是在它们相交处添加的值(ra.region 2 将包括坐标 506 和 509,并添加 rb.region 3 的坐标:506 和 508 ,在坐标 506 处添加)。 507 没有出现在任何一个区域 ID 中,因此它是 0 或 null,以两者为准。

最佳答案

如果理解正确(我不确定)你的查询可能看起来像

SELECT COALESCE(b.gps_coords, c.gps_coords) AS gps_coords,
       COALESCE(b.year, c.year) AS year,
       COALESCE(b.value, 0) + COALESCE(c.value, 0) AS value
  FROM
(
  SELECT d.gps_coords, d.year, SUM(d.value) AS value
    FROM data d JOIN regions r
      ON d.gps_coords = r.gps_coords
   WHERE d.metric = 5 AND r.region_id = 1
   GROUP BY d.gps_coords, d.year
) b FULL JOIN
(
  SELECT d.gps_coords, d.year, SUM(d.value) AS value
    FROM data d JOIN regions r
      ON d.gps_coords = r.gps_coords
   WHERE (d.metric = 4 AND r.region_id = 3)
   GROUP BY d.gps_coords, d.year
) c
    ON b.gps_coords = c.gps_coords
   AND b.year = c.year
 ORDER BY gps_coords

示例输出:

| GPS_COORDS | YEAR |  VALUE |
-------------|------|--------|
|        506 | 2010 | 236.76 |
|        507 | 2010 |  10.32 |
|        508 | 2010 |  462.9 |
|        509 | 2010 |  45.24 |

这是 SQLFiddle 演示

关于sql - 如何对单独的查询集执行数学公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18434156/

相关文章:

mysql - IIS 站点被黑客入侵?

sql - 需要使用 JOIN 优化 SQL 查询的技巧

apache - 使用 LDAP 的 Cloudera Sentry - 无法将用户添加为 Sentry 管理员

sql - Postgres 中同月的总和值

function - 在执行 postgresql 函数时提交事务

sql - 使用 select * 的递归查询引发 ORA-01789

sql - 检查 SYS_REFCURSOR 是否为空的最佳方法

sql - 删除全局临时表不会删除其索引

postgresql - pgAdmin 查询工具文本高亮显示

php - 如果密码包含空格,PDO 无法连接