1

我在查询中遇到错误。此查询正常并返回数据(选择和行数用于分页):

select *
from (select a.*, rownum rnum
from (select id_edition, id_document, name, extension, creation_date, url, 
(select inscription_date from edition_student_d0 where id_edition = 12345 and id_third =     12345) inscription_date
from upd_edition_doc_d0
where id_edition = 1071591
order by creation_date desc) a
where rownum <= 10 )
where rnum >= 1

现在我试图包含一个“case when”并仅在某些情况下获取 url,所以我进行了这些修改,包括一个 case 块:

select *
from (select a.*, rownum rnum
from (select id_edition, id_document, name, extension, creation_date, 
(select inscription_date from edition_student_d0 where id_edition = 12345 and id_third = 12345) inscription_date,
case
when trunc(inscription_date + 90) <= trunc(sysdate) then null
else url
end url
from upd_edition_doc_d0
where id_edition = 1071591
order by creation_date desc) a
where rownum <= 10 )
where rnum >= 1

Oracle 启动此错误:

ORA-00904: "INSCRIPTION_DATE": invalid identifier

我想那是因为我要求 inscription_date 并在同一查询级别使用它,但我不知道如何处理这个问题。

另外,我怎样才能做出我想做的东西?我的意思是,仅在特定条件下获取 url。

有人可以帮忙吗?

先感谢您。

4

1 回答 1

2

您不能在同一级别的查询中引用别名。

您可以替换子查询...

select *
from (select a.*, rownum rnum
  from (select id_edition, id_document, name, extension, creation_date, 
           (select inscription_date from edition_student_d0 where id_edition = 12345 and  id_third = 12345) inscription_date,
           case when trunc((select inscription_date from edition_student_d0 where id_edition = 12345 and  id_third = 12345) + 90) <= trunc(sysdate) then null
             else url
           end as url
           from upd_edition_doc_d0
           where id_edition = 1071591
           order by creation_date desc) a
   where rownum <= 10 )
where rnum >= 1

或者将箱子上移一层。

select *
from (select a.*, 
      case when trunc(inscription_date + 90) <= trunc(sysdate) then null
       else url
      end as url,
      rownum rnum
  from (select id_edition, id_document, name, extension, creation_date, 
           (select inscription_date from edition_student_d0 where id_edition = 12345 and  id_third = 12345) inscription_date
           from upd_edition_doc_d0
           where id_edition = 1071591
           order by creation_date desc) a
   where rownum <= 10 )
where rnum >= 1
于 2011-09-20T08:37:50.653 回答