ORA-00923:在预期的地方找不到FROM关键字(检索部门明智的最高2工资)

时间:2017-10-26 22:57:26

标签: sql oracle12c

我试图检索每个部门最高的两个工资。但是我收到了错误!请帮助,我将不胜感激,谢谢。

CREATE TABLE emp_h (    Dept varchar(100),  Emp varchar(100),   Sal numeric(18,2) )

在下表中插入值。

SELECT * FROM emp_h

    DEPT    EMP SAL
    Comp    A   6000
    Comp    B   7000
    Comp    C   10000
    Comp    D   9000
    Elec    P   2000
    Elec    Q   10000
    Elec    R   11000
    Eng     AA  15000
    Eng     BB  2000
    Eng     BB  7000
    Eng     BB  3000

我想要输出(部门明智的前2名最高薪水)

Dept    Emp Sal
----------------------------
Comp    C   10000
Comp    D   9000
Elec    R   11000
Elec    Q   10000
Eng     AA  15000
Eng     BB  7000

现在我运行此查询以检索每个部门最高的2个工资:

SELECT * FROM(SELECT RANK() OVER (PARTITION BY DEPT ORDER BY SAL DESC) AS 'Rank', * FROM emp_h) AS A WHERE RANK <= 2

结果:

ORA-00923: FROM keyword not found where expected
00923. 00000 -  "FROM keyword not found where expected"
*Cause:    
*Action:
Error at Line: 25 Column: 61

1 个答案:

答案 0 :(得分:-1)

如果该行上还有其他列,则Oracle希望您符合*的条件。所以试试这个:

SELECT e.*
FROM (SELECT RANK() OVER (PARTITION BY DEPT ORDER BY SAL DESC) AS rnk, e.* 
      FROM emp_h e
      ) e
WHERE rnk <= 2;