在Oracle的VIEW中编码COUNT

时间:2016-11-18 19:23:17

标签: oracle

我陷入了Oracle的编程问题,我正在努力解决。我想创建一个View,它将在2016年10月27日FBN001上给出我未预留/可用座位的数量

这是飞行表:

CREATE TABLE Flight(
  Flight_ID VARCHAR(25),
  Route_ID VARCHAR(25),
  Airplane_ID VARCHAR(10),
  Departure_Time DATE,
  Arrival_Time DATE,
    CONSTRAINT FlightIDPK PRIMARY KEY(Flight_ID),
    CONSTRAINT RouteIDFK FOREIGN KEY(Route_ID) REFERENCES Route(Route_ID),
    CONSTRAINT AirplaneIDFK FOREIGN KEY(Airplane_ID) REFERENCES Airplane(Airplane_ID));

这是飞机表:

CREATE TABLE Airplane(
  Airplane_ID VARCHAR(10),
  Capacity NUMBER(5) NOT NULL,
  Model VARCHAR(15) NOT NULL,
    CONSTRAINT AirplaneIDPK PRIMARY KEY(Airplane_ID));

这是预订表:

CREATE TABLE Booking(
  Booking_ID NUMBER(10),
  Flight_ID VARCHAR(25),
  Customer_ID NUMBER(10),
  Payment_Method VARCHAR(10) NOT NULL,
  CreditCard_Details NUMBER(18),
    CONSTRAINT BookingIDPK PRIMARY KEY(Booking_ID),
    CONSTRAINT FlightIDFK FOREIGN KEY(Flight_ID) REFERENCES Flight(Flight_ID),
    CONSTRAINT CustomerIDFK FOREIGN KEY(Customer_ID) REFERENCES Customer(Customer_ID));

我以为我会使用COUNT函数计算预订

CREATE VIEW ViewB AS(
SELECT Flight.Route_ID, Flight.Departure_Time, Airplane.Capacity, COUNT(Flight_ID) AS NumberOfBooking
FROM Flight, Airplane, Booking
WHERE Flight.Airplane_ID = Airplane.Airplane_ID
AND Flight.Route_ID = 'FBN001'
GROUP BY Flight.Route_ID, Airplane.Capacity); 

但那不起作用,

它给了我错误

错误报告 -

SQL错误:ORA-00918:列模糊定义

  1. 00000 - “列模糊定义”
  2. *原因:

    *动作:

    可能是什么解决方案? 谢谢

1 个答案:

答案 0 :(得分:0)

您没有添加声明以正确加入预订表:

CREATE VIEW ViewB AS(
SELECT Flight.Route_ID, Flight.Departure_Time, Airplane.Capacity,     COUNT(Flight_ID) AS NumberOfBooking
FROM Flight, Airplane, Booking
WHERE Flight.Airplane_ID = Airplane.Airplane_ID
AND Flight.Flight_ID=Booking.Flight_ID
AND Flight.Route_ID = 'FBN001'
GROUP BY Flight.Route_ID, Airplane.Capacity);