简体   繁体   English

使用别名访问Oracle ROWNUMBER

[英]Accesing Oracle ROWNUMBER with alias name

I have a Oracle Query 我有一个Oracle查询

WITH cte AS (
SELECT

    category_id,
    category_name,
    parent_category_id,
    ltrim(sys_connect_by_path(category_name, '/'), '/') "ParentNames"       
FROM
    bg_categories
START WITH
    parent_category_id = - 1
CONNECT BY NOCYCLE
    PRIOR category_id = parent_category_id
 )
 SELECT
ROW_NUMBER() OVER (
    ORDER BY 
      bco.ID desc
  ) AS SLNO,
bco.*,
cte.*,

(
    SELECT
        COUNT(*)
    FROM
        object_document obj
    WHERE
        obj.object_id = bco.id
        AND object_type_id = 85
) AS customobjects_doc_count 
 FROM
bg_custom_objects bco
JOIN cte ON ( cte.category_id = bco.category_id )  where SLNO between 10 and 25

This query works fine with out this 'where SLNO between 10 and 25' where statement at the end , but with this it's showing "SLNO invalid identifier " error. 此查询在末尾显示“'SLNO在10到25之间”的where语句时可以很好地工作,但是显示“ SLNO无效标识符”错误。

Pls help. 请帮助。

You need to wrap it with inline view/cte: 您需要使用内联视图/ cte包装它:

WITH cte AS (
  SELECT
    category_id,
    category_name,
    parent_category_id,
    ltrim(sys_connect_by_path(category_name, '/'), '/') "ParentNames"       
  FROM bg_categories
  START WITH parent_category_id = - 1
  CONNECT BY NOCYCLE
  PRIOR category_id = parent_category_id
), cte2 AS (
  SELECT
   ROW_NUMBER() OVER (ORDER BY bco.ID desc) AS SLNO
      --,bco.* -- columns should be listed explicitly
      --,cte.* -- columns should be listed explicitly
  ,(
    SELECT COUNT(*)
    FROM object_document obj
    WHERE obj.object_id = bco.id
      AND object_type_id = 85
  ) AS customobjects_doc_count 
  FROM bg_custom_objects bco
  JOIN cte ON ( cte.category_id = bco.category_id )
)
SELECT *
FROM cte2
WHERE SLNO BETWEEN 10 AND 25;

You cannot use aliases defined directly in SELECT in WHERE clause if both are on the same level because it is not visible Can't refer to column counting the ocurrences 如果两者都处于同一级别,则不能使用直接在SELECT中的WHERE子句中定义的别名,因为它们不可见无法引用计数发生次数的列

Additionally you could not move ROW_NUMBER to WHERE either Why no windowed functions in where clauses? 另外,您也无法将ROW_NUMBER移到WHERE为什么where子句中没有窗口函数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM