分区上的行号错误

时间:2014-11-18 11:09:00

标签: oracle

我正在创建一个子查询来选择特定列上的不同条目DIS_COL,然后返回这些不同条目的所有其他列,任意选择第一行。

要做到这一点,我正在创建一个子查询,它只使用over-partition by选择第一行,然后从该子查询中选择。

但是我的代码出错了; “ORA-00923:未找到FROM关键字”。

我的代码如下:

select *
from (
   select *,
          row_number() over (partition by DIS_COL order by COL_2) as row_number --ORDER BY FIELD DETERMINES WHICH ROW IS THE FIRST ROW AND THUS WHICH ONE IS SELECTED.
   from MY_TABLE
   ) as rows
where row_number = 1
AND CRITERIA_COL = 'CRIT_1'
OR CRITERIA_COL_2 = 'CRIT_2';

如何更正我的代码以达到预期效果?

我正在研究Oracle数据库。

2 个答案:

答案 0 :(得分:3)

删除as rows。它不是表/查询别名的正确语法。它是列别名的语法。

select *
from (
   select T.*, 
          row_number() over (partition by DIS_COL order by COL_2) as row_number --ORDER BY FIELD DETERMINES WHICH ROW IS THE FIRST ROW AND THUS WHICH ONE IS SELECTED.
   from MY_TABLE t
   ) 
where row_number = 1
AND (CRITERIA_COL = 'CRIT_1'
OR CRITERIA_COL_2 = 'CRIT_2');

答案 1 :(得分:0)

它不是ROW_NUMBER,它是*,为子查询添加别名:

select *
from (
   select T.*, -- here
          row_number() over (partition by DIS_COL order by COL_2) as row_number --ORDER BY FIELD DETERMINES WHICH ROW IS THE FIRST ROW AND THUS WHICH ONE IS SELECTED.
   from MY_TABLE
   )T as rows -- and here
where row_number = 1
AND CRITERIA_COL = 'CRIT_1'
OR CRITERIA_COL_2 = 'CRIT_2';