如何在SQL中修复缺少的右括号

时间:2019-06-15 09:06:23

标签: sql oracle

在解决此问题时,我遇到了正确的括号问题: 列出该月特定月份的司机姓名,年龄,位置,平均旅行等级

这是我在SQL中尝试过的方法。

CREATE TABLE driver (
    driverID NUMBER(4) PRIMARY KEY, 
    Dname VARCHAR2(20),
    DNRIC CHAR(14),
    Dgender CHAR(1),
    DDOB DATE,
    Dmobile CHAR(11),
    DcarNo CHAR(6),
    Dstart DATE,
    Dstop DATE,
    Dgrade CHAR(6),
    DLicence CHAR(4)
 );


CREATE TABLE booking (
    BookID NUMBER(4) PRIMARY KEY,
    PID NUMBER(4),
    driverID NUMBER(4),    
       PickLoc VARCHAR2(13),
       DropLoc VARCHAR2(13),
       TDate DATE,
       TTime NUMBER(4),
       RideFare CHAR(6),
       TollOther CHAR(6),
    cancelDate DATE,
       TripRating NUMBER(1), 
    PayMethod CHAR(15),
 FOREIGN KEY (PID) references passenger (PID),
 FOREIGN KEY (driverID) references driver (driverID)
 );



 SELECT Dname as driver, 
 floor(months_between(sysdate,driver.DDOB)/12) as age,
 Dgrade as position,
 rating
 FROM driver
 LEFT JOIN 
 (SELECT driverID, avg(TripRating) as rating
 FROM booking group by driverID
 where (TDate between '01-Apr-2019' and '30-Apr-2019')
 order by rating) 
 booking on driver.driverID = booking.bookingID

缺少右括号。此外,我想在四月份获得驾驶员的平均评分

1 个答案:

答案 0 :(得分:0)

Oracle不允许在这样的子查询中使用order by子句(因为无论如何它都是毫无意义的),并且在您尝试时会给出非常讨厌的违反直觉的ORA-00907错误。移动order by子句应该可以解决问题。另外,请注意,where子句应该位于group by子句之前,而不是之后:

SELECT     dname AS driver, 
           FLOOR(MONTHS_BETWEEN(SYSDATE, driver.ddob) / 12) AS age,
           dgrade AS position,
           rating
FROM       driver
LEFT JOIN (SELECT   driverid,
                    AVG(triprating) AS rating
           FROM     booking
           WHERE    tdate BETWEEN '01-Apr-2019' AND '30-Apr-2019'
           GROUP BY driverid) booking ON driver.driverid = booking.driverid
ORDER BY  rating -- Moved here