2

这是我创建的表:

CREATE TABLE Plane_new (Pnum char(3), Feature varchar2(20), Ptype varchar2(15), 
primary key (Pnum));

CREATE TABLE Employee_new (eid char(3), ename varchar(10), salary number(7,2), mid char(3),
PRIMARY KEY (eid), FOREIGN KEY (mid) REFERENCES Employee_new);

CREATE TABLE Pilot_new (eid char(3), Licence char(9), primary key (eid), 
foreign key (eid) references Employee_new on delete cascade);

CREATE TABLE FlightI_new (Fnum char(4), Fdate date, Duration number(2), Pid char(3), 
Pnum char(3), primary key (Fnum), foreign key (Pid) references Pilot_new (eid), 
foreign key (Pnum) references Plane_new);

这是我必须完成的查询:对于每个航班,显示其编号、执行该航班的飞行员的姓名,如果飞行持续时间长于平均水平或“短于平均水平”,则显示“长于平均”字样如果飞行时间短于或等于平均值​​。对于包含“长于平均值”或“短于平均值”的列,请制作标题长度。

这是我想出的-没有运气!

SELECT F.Fnum, E.ename,
CASE Length 
WHEN F.Duration>(SELECT AVG(F.Duration) FROM FlightI_new F)
THEN "Longer than average"
WHEN F.Duration<=(SELECT AVG(F.Duration) FROM FlightI_new F)
THEN 'Shorter than average' END
FROM FlightI_new F LEFT OUTER JOIN Employee_new E ON F.Pid=E.eid
GROUP BY F.Fnum, E.ename;

我哪里错了?

4

3 回答 3

0

请您看一下 * SQLFiddle中的这个示例查询 也许它或多或少是一个语法问题。在 Case 语句的末尾使用Case Length而不是使用。As Length不过,在优化方面,我没有对您的查询进行任何更改。

SELECT F.Fnum, E.ename,
CASE 
WHEN F.Duration>(SELECT AVG(F.Duration) 
                 FROM FlightI_new F)
THEN Longer than average
WHEN F.Duration<=(SELECT AVG(F.Duration) 
                  FROM FlightI_new F)
THEN 'Shorter than average' 
END as Length
FROM FlightI_new F 
LEFT OUTER JOIN Employee_new E 
ON F.FPid=E.eid

根据我使用的样本数据提供以下结果。我仍然不确定您的“中间”列FOREIGN KEY (mid) REFERENCES Employee_new);是否真的引用了任何地方......所以我删除了引用完整性并将其单独放置。

FNUM    ENAME   LENGTH
f001    kaen    Longer than average
f002    jerry   Longer than average
f003    dane    Longer than average
f004    jerry   Shorter than average
f005    kaen    Shorter than average
f006    tim     Shorter than average
f007    dane    Shorter than average
于 2012-11-21T05:06:45.550 回答
0
SELECT F.Fnum, E.ename,
(CASE F.Duration 
WHEN F.Duration>(SELECT AVG(B.Duration) FROM FlightI_new B)
THEN 'Longer than average'
ELSE 'Shorter than average' END) AS LENGTH
FROM FlightI_new F LEFT OUTER JOIN Employee_new E ON F.Pid=E.eid
GROUP BY F.Fnum, E.ename;
于 2012-11-21T04:33:15.750 回答
0

替换这一行:

THEN "Longer than average"

用这条线:

THEN 'Longer than average'

换句话说,将双引号更改为单引号。

于 2012-11-21T04:35:44.950 回答