Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL跨表查询以标识特定属性的聚合值_Sql_Postgresql - Fatal编程技术网

SQL跨表查询以标识特定属性的聚合值

SQL跨表查询以标识特定属性的聚合值,sql,postgresql,Sql,Postgresql,我正试图查询沿停、卸、载更多乘客的航线飞行的飞机上的可用座位 我一直在尝试跨表查询,但在访问其他子查询范围之外的单独子查询中的其他查询表的属性时遇到问题。这是否可能与with_____;AS查询有关 这里的前两个表是我正在使用的数据示例,希望能够产生预期的结果。 下面的第一张表是一系列乘客的中转线路,这些乘客在斯特胡机场乘坐329号飞机,然后在斯特胡机场下车 start_airport | end_airport ---------------+------------

我正试图查询沿停、卸、载更多乘客的航线飞行的飞机上的可用座位

我一直在尝试跨表查询,但在访问其他子查询范围之外的单独子查询中的其他查询表的属性时遇到问题。这是否可能与with_____;AS查询有关

这里的前两个表是我正在使用的数据示例,希望能够产生预期的结果。 下面的第一张表是一系列乘客的中转线路,这些乘客在斯特胡机场乘坐329号飞机,然后在斯特胡机场下车

 start_airport | end_airport
---------------+------------
            78 |          76
            78 |          76
            78 |          74
            77 |          76
            77 |          76
            77 |          75
            77 |          75
            77 |          75
            77 |          74
            77 |          74
            76 |          75
            76 |          75
            76 |          75
            76 |          75
            76 |          75
            76 |          74
            75 |          74
            75 |          74
            75 |          74
            75 |          74
            75 |          74

 airplane | airport_id | airplane_size
----------+------------+-------------
      329 |         78 |           67
      329 |         77 |           67
      329 |         76 |           67
      329 |         75 |           67
      329 |         74 |           67
对于这组特定数据,我想要得到的表格是,路线上每个站点的可用座位都会更新:

 airplane | airport_id | available_seating
----------+------------+------------------
      329 |         78 |               64
      329 |         77 |               57
      329 |         76 |               55
      329 |         75 |               59

您的问题可以通过从
连接
将开始机场和结束机场的总和向左连接来解决<代码>求和分析函数可以为给定表达式生成累积结果

您唯一需要的,您还没有显示的是一个id列,它指示停止顺序的顺序。我刚刚使用了
ID
作为一个整数序列,如果您有日期列,它也可能是一个日期列。如果没有任何这样的列,就不可能确定运行总和的方向

WITH st AS (
     SELECT start_airport,
            COUNT(*) AS cnt
     FROM connections
     GROUP BY start_airport
),en AS (
     SELECT end_airport,
            COUNT(*) AS cnt
     FROM connections
     GROUP BY end_airport
) SELECT airplane,
         airport_id,
         airplane_size - SUM( coalesce( st.cnt ,0) - coalesce(en.cnt,0) ) 
 OVER( ORDER BY id --You should use the appropriate id column that determines order.
                   ) as available_seating
  FROM stops t2 left
      JOIN st ON t2.airport_id = st.start_airport
  LEFT JOIN en ON t2.airport_id = en.end_airport

您的问题可以通过从
连接将开始机场和结束机场的总和向左连接来解决<代码>求和
分析函数可以为给定表达式生成累积结果

您唯一需要的,您还没有显示的是一个id列,它指示停止顺序的顺序。我刚刚使用了
ID
作为一个整数序列,如果您有日期列,它也可能是一个日期列。如果没有任何这样的列,就不可能确定运行总和的方向

WITH st AS (
     SELECT start_airport,
            COUNT(*) AS cnt
     FROM connections
     GROUP BY start_airport
),en AS (
     SELECT end_airport,
            COUNT(*) AS cnt
     FROM connections
     GROUP BY end_airport
) SELECT airplane,
         airport_id,
         airplane_size - SUM( coalesce( st.cnt ,0) - coalesce(en.cnt,0) ) 
 OVER( ORDER BY id --You should use the appropriate id column that determines order.
                   ) as available_seating
  FROM stops t2 left
      JOIN st ON t2.airport_id = st.start_airport
  LEFT JOIN en ON t2.airport_id = en.end_airport

您如何确定应首先统计哪个机场?有没有其他的列决定机场的顺序考虑,比如说77是78之后?你如何确定哪个机场应该首先计算?有没有其他列来决定机场的顺序,比如说77是78后?