【问题标题】:How to get count of two columns of a table based on PK of another table?如何根据另一个表的 PK 计算一个表的两列?
【发布时间】:2019-07-11 03:56:36
【问题描述】:

我有两个表定义为:

CREATE TABLE airports (
  code char(3) not null primary key,
  name varchar(100) not null,
  city varchar(50) not null,
  state varchar(5),
  country char(2) not null references fcountries(code)
)

CREATE TABLE flights (
  departure char(3) not null references airports(code), -- airport code
  arrival char(3) not null references airports(code), -- airport code
  dep_time time not null,
  airline char(2) not null references airlines(code)
)

我需要使用 postgres 获取从机场出发和到达机场的航班数量。所以,我写了

select
  code,
  count(departure)
from airports 
join flights 
 on flights.departure = airports.code group by code;

对于出发也是如此,

select
  code,
  count(arrival) as Arrival
from airports 
join flights 
  on flights.arrival = airports.code group by code;

但我需要将这两者结合起来,并在相同的结果中获得到达-离开计数。我不知道该怎么做? 我写了这个查询

select
  code,
  count(departure),
  x.arrival
from (
  select count(arrival) as arrival
  from airports 
  join flights 
    on flights.arrival = airports.code group by code
) x, 
airports 
join flights on airports.code = flights.departure
group by code, x.arrival
order by code;

但结果不正确,因为在此结果中重复了“代码”。我对 sql 很陌生,不知道怎么写。

【问题讨论】:

  • 更新您的问题,添加相关表架构一个适当的数据样本和预期结果
  • 请决定 DBMS。
  • 我猜错了。 group by code 而不是 roup by code
  • 我改了还是有错误。

标签: mysql sql


【解决方案1】:

您可以将这两个查询用作子查询并加入

    select t1.code,  t1.count_departure, t2.count_arrival 
    from (
        select code, count(departure)  count_departure
        from airports join flights on flights.departure = airports.code 
        group by code

    ) t1  
    inner join  (
        select code, count(arrival) as  count_arrival 
        from airports join flights on flights.arrival = airports.code 
        roup by code

    ) t2 on t1.code  = t2.code ;

【讨论】:

  • 这给出了错误“选择附近的语法错误”,即在 t2 之后选择(选择代码....)我不知道为什么?
  • 答案已更新.. 别名的位置错误
【解决方案2】:

我认为别名 t2 不能在加入后那样使用,这就是您收到错误的原因。试试这个:

select t1.code, t1.count_departure, t2.count_arrival from ( select code, count(departure) count_departure from airports join flights on flights.departure = airports.code group by code ) t1 join ( select code, count(arrival) as count_arrival from airports join flights on flights.arrival = airports.code group by code ) t2 on t1.code = t2.code;

【讨论】:

    【解决方案3】:

    最简单的方法是横向连接和group by:

    select v.code, count(*) as total,
           sum(is_departure) as num_departures,
           sum(is_arrival) as num_arrivals
    from flights cross join lateral
         (values (departure, 1, 0), (arrival, 0, 1)
         ) as v(code, is_departure, is_rrival)
         on f.departure = a.code
    group by v.code;
    

    请注意,JOINairports 是不必要的,除非您确实想要引入更多您在问题中未提及的信息。

    【讨论】:

      【解决方案4】:

      一个非常简单的方法就是在 SELECT 子句中使用相关的 COUNT 子查询:

      select a.code,
          (select count(*) from flights f where f.departure = a.code) as departures,
          (select count(*) from flights f where f.arrival = a.code) as arrivals
      from airports a;
      

      【讨论】:

        猜你喜欢
        • 2013-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-14
        相关资源
        最近更新 更多