oracle PL/SQL中的行号函数

ulydmbyx  于 2023-05-06  发布在  Oracle
关注(0)|答案(2)|浏览(222)

我试图从一个表中使用cte和行号函数获取特定数量的记录。每当我运行查询时,我都会得到一个关于查询中“FROM”子句位置的错误。请提出这个错误背后的原因,因为我是新的SQL。
错误:
ORA-00923:在预期的00923位置未找到FROM关键字。00000 -“FROM关键字未在预期位置找到”* 原因:

  • 操作:行出错:3柱:10**

验证码:

WITH cte2 AS (
    SELECT
        *, row_number() over (order by a.v_prt_nbr_spr) as rw_number
    FROM
        cf_active_pn_master a
        INNER JOIN cf_conn_support b ON a.obj_id = b.obj_id
    WHERE
        a.v_prt_nbr_spr LIKE 'HQCD%'
)
SELECT *
FROM cte2
where rw_number between 5000 and 10000;
zz2j4svz

zz2j4svz1#

要将*与添加到SELECT列表中的任何其他内容一起使用,您必须对*进行别名:

WITH cte2 AS (
    SELECT
        a.*, row_number() over (order by a.v_prt_nbr_spr) as rw_number
    FROM cf_active_pn_master a . . .

如果你想要ab列,你必须至少列出其中一个,因为a.*,b.*将返回重复的obj_id列,而这在查询块中是不允许的,因为它是另一个块的视图。所以:

WITH cte2 AS (
    SELECT
        a.*, 
        b.col1,
        b.col2,
        b.col3,
        row_number() over (order by a.v_prt_nbr_spr) as rw_number
    FROM
        cf_active_pn_master a
        INNER JOIN cf_conn_support b ON a.obj_id = b.obj_id 
        . . .
6yt4nkrj

6yt4nkrj2#

这是针对emp示例表的类似查询:

with cte2 as 
(
  select *
        ,row_number()
          over(
     order by hiredate desc
          ) as rw_number
    from emp e
)
select * from cte2 where rw_number between 5 and 10;

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

请注意,错误与您的错误相同。如果您从多个表中选择*,或者您选择了所有内容,然后选择了另一列,则需要对*进行别名。

with cte2 as 
(
  select e.*
        ,row_number()
          over(
     order by hiredate desc
          ) as rw_number
    from emp e
)
select * from cte2 where rw_number between 5 and 10;

相关问题