Oracle group by子句在select语句中使用嵌套函数

时间:2014-10-16 07:24:28

标签: sql oracle

我有一个查询,我需要找到一个航班的可用座位。即:从分配给该航班的飞机的容量中减去为航班预订的次数。我有一个查询工作,但它只显示'可用席位'列。我想显示“航班号”列,但是当我尝试将列添加到显示中时,我得到“不是单组组功能”等错误。我想我需要加入表格使select语句允许我打印flight_number列,但我不知道该怎么做。有人能指出我正确的方向。非常感谢。

select sum(p.capacity - (count(b.passenger_id))) as available_seats from booking b, PLANES p
where b.date_of_flight = '16-Oct-2014' and depart_city = 'Sydney' and arrival_city = 'Perth'
and flight_number in (select flight_number from scheduled_flights sf 
                      where sf.airplane_serial = p.airplane_serial
                      and b.date_of_flight = sf.date_of_flight
                      )
group by p.capacity 
;

结果如下:

      available_seats
1       237

这是正确的,但我想:

     flight_number  available_seats
1      TF81              237
感谢您的帮助: - )

2 个答案:

答案 0 :(得分:0)

也许你需要这样的东西?

select flight_number, p.capacity - count(b.passenger_id) as available_seats 
 from booking b, PLANES p, scheduled_flights sf
where b.date_of_flight = '16-Oct-2014' and depart_city = 'Sydney' and arrival_city = 'Perth'
  and flight_number = sf.flight_number
  and sf.airplane_serial = p.airplane_serial
  and b.date_of_flight = sf.date_of_flight
group by flight_number, p.capacity;

答案 1 :(得分:0)

您可以在flight_number上使用另一个聚合函数,例如" max(flight_number)"在SELECT子句中。它不会改变你的逻辑,你不应该再犯这个错误了。