我试图通过汽车价格找到最近的汽车价格到表中的平均汽车价格,但以下情况给我一个错误ORA-00923: FROM keyword not found where expected
select *
from
(
select *, abs(CAR_PRICE - (select avg(CAR_PRICE) from USER.CAR)) as PriceDif
from USER.CAR
order by PriceDif
)
where rownum = 1;
答案 0 :(得分:1)
您可以使用不合格的*
,但前提是您还没有添加其他列。如果您(正如您所见,在内部查询中),您必须限定*
,如USER.CAR.*
在内部查询中,在Oracle读取select *
后,它期望from
},而不是逗号。请使用select USER.CAR.*, abs(.....)
概念证明(从不同架构运行,引用标准SCOTT架构)
select *
from ( select scott.emp.*, abs(sal - (select avg(sal) from scott.emp)) as sal_diff
from scott.emp
order by sal_diff
)
where rownum = 1
;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO SAL_DIFF
7782 CLARK MANAGER 7839 06-09-1981 2450 10 376.78571
答案 1 :(得分:1)
您可以使用分析函数来摆脱嵌套的子查询,只需要执行单个表扫描:
SELECT *
FROM (
SELECT *
FROM (
SELECT e.*,
AVG( salary ) OVER () AS avg_salary
FROM HR.Employees e
)
ORDER BY ABS( salary - avg_salary )
)
WHERE ROWNUM = 1;
(使用Oracle的HR模式的示例)