简体   繁体   English

连接并比较2个表的2个查询

[英]Join and compare 2 queries of 2 tables

This is probably a quite trivial question for many here but I am not used to write sub queries and joins, so I hope someone want to help. 对于这里的许多人来说,这可能是一个相当琐碎的问题,但是我不习惯编写子查询和联接,因此我希望有人希望提供帮助。

I have two tables: new_road and old_roads. 我有两个表:new_road和old_roads。

These two queries sum up the length of the roads belonging to a specific road number. 这两个查询汇总了属于特定道路编号的道路长度。

SELECT new_road.nummer, SUM(new_road.length) FROM road_table.road GROUP BY new_road.nummer

SELECT old_road.nummer, SUM(ST_length(old_road.geom)) FROM old_road_table.old_road GROUP BY old_road.nummer

I wish to have a result table where these two queries are joined so I can compare the new and old summed length for each road number. 我希望有一个将这两个查询结合在一起的结果表,以便我可以比较每个道路编号的新旧总和长度。

Like 喜欢

old.nummer old.length new.nummer new.lenght
2345       10.3       2345       10.5      
2346       578.2      2346       600
2347       54.2       NULL       NULL
NULL       NULL       2546       32.2

I think some version of an outer join is needed because there will be a road numbers in the old_road table that does not exist in the new.road table and i would like to see them too. 我认为需要某种形式的外部联接,因为在old.road表中会有一个路号,而new.road表中不存在,我也希望看到它们。

Appreciate any advice 感谢任何建议

Edit: 编辑:

After advice from below did I came up with this: 经过下面的建议,我想到了:

SELECT * FROM
(SELECT new_road.nummer, SUM(new_road.length) FROM road_table.road GROUP BY new_road.nummer) new_table
FULL OUTER JOIN
(SELECT old_road.nummer, SUM(ST_length(old_road.geom)) FROM old_road_table.old_road GROUP BY old_road.nummer) old_table
ON new_road.nummer = old_road.nummer

But each time I run it I get missing FROM-clause entry. 但是每次运行它时,都会丢失FROM子句条目。 When I run each sub query individually they work. 当我分别运行每个子查询时,它们会起作用。 I have crosschecked with the documentation and it look OK to me, but clearly I am missing something here. 我已经对文档进行了交叉核对,对我来说看起来还可以,但是显然我在这里遗漏了一些东西。

Consider using a FULL OUTER JOIN 考虑使用FULL OUTER JOIN

This is not the exact output you requested but you don't need to display the nummer twice. 这不是您要求的确切输出,但是您不需要两次显示数字。

SELECT
    COALESCE(new_road.nummer,old_road.nummer)nummer,
    new_road.length,
    old_road.length
FROM (
    SELECT new_road.nummer
        ,SUM(new_road.length) length
    FROM road_table.road
    GROUP BY new_road.nummer
) new_road 
FULL OUTER JOIN (
    SELECT old_road.nummer
        ,SUM(ST_length(old_road.geom))length
    FROM old_road_table.old_road
    GROUP BY old_road.nummer
) old_road ON
    old_road.nummer = new_road.nummer

Following query should solve the purpose. 以下查询应解决目的。 I didn't run it but the basic idea is result of a query on a table is another table on which you can query again. 我没有运行它,但是基本思想是在一个表上查询的结果是另一个可以在其上查询的表。

Select * FROM (SELECT new_road.nummer, SUM(new_road.length) FROM road_table.road GROUP BY new_road.nummer) table1 JOIN (SELECT old_road.nummer, SUM(ST_length(old_road.geom)) FROM old_road_table.old_road GROUP BY old_road.nummer) table2 ON table1.new_road.nummer = table2.old_road.nummer

The tricky bit here is that you want to make sure you include all of the keys from both lists. 棘手的一点是,您要确保同时包含两个列表中的所有键。 My favorite way to do this kind of thing is: 我最喜欢的做这种事情的方法是:

select * from (
  SELECT distinct new_road.nummer as nummer from road_table.road
  union
  SELECT distinct old_road.nummer as nummer FROM old_road_table.old_road
) allkeys
left join
(
  SELECT new_road.nummer as nummer, SUM(new_road.length) as nlen
  FROM road_table.road GROUP BY new_road.nummer
) n 
  on allkeys.nummer = n.nummer
left join 
(
  SELECT old_road.nummer as nummer, SUM(ST_length(old_road.geom)) as olen
  FROM old_road_table.old_road GROUP BY old_road.nummer
) o
  on allkeys.nummer = o.nummer

The first subquery builds a list of all keys, then you join to both of your queries from there. 第一个子查询建立所有键的列表,然后从那里加入两个查询。 There's nothing wrong with an outer join, but I find this easier to manage if you have to include 3 or more tables. 外部联接没有什么问题,但是我发现如果必须包含3个或更多表,这将更易于管理。 If you had to include another table it would just be one more union in allkeys and one more left join to that table. 如果您必须包括另一个表,则它在allkey中只是一个并集,而向该表的左连接又是一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM