【问题标题】:ORA-00904: Invalid Identifier on Oracle queryORA-00904: Oracle 查询的标识符无效
【发布时间】:2023-01-12 18:12:17
【问题描述】:

您好有以下查询,但是当我执行查询时,我在 oracle 中遇到错误。问题似乎出在 ORDER BY 上,但我不确定为什么会有无效标识符,因为我的列清楚地出现在表中。我正在使用 sqldeveloper 来执行查询。

ORA-00904: "FINALRESULTS"."TESTTIME": 无效标识符

WITH tests AS (
 SELECT 
   t.place, 
   t.stationnr, 
   t.result, 
   i.type, 
   t.time,  
 FROM 
   result_test t 
   INNER JOIN result_ident i ON t.place= i.place
   JOIN stat_testplacegroup pg on pg.place = t.place 
   INNER JOIN result_release r ON i.type= r.type
 WHERE 
   (
     t.testtime BETWEEN to_date(
       '2022-11-20T00:00:00', 'YYYY-MM-DD"T"HH24:MI:SS
'
     ) 
     AND to_date(
       '2022-11-26T00:00:00', 'YYYY-MM-DD"T"HH24:MI:SS'
     )
   )
), 
finalresults AS (
 SELECT 
   t.place "place", 
   t.stationnr "stationnr", 
   convres(t.result) "convres", 
   t.type "type", 
   t.testtime "testtime"
 FROM 
   tests t 
   INNER JOIN result_testecu_11 tecu ON tecu.testresid = t.testresid 
   INNER JOIN result_pathecu pecu ON pecu.ecuid = tecu.ecuid 
   INNER JOIN result_pathfunc func ON pecu.ecuid = func.ecuid 

 GROUP BY  
   t.place, 
   t.stationnr, 
   t.type, 
   t.testtime
   t.result
) 
SELECT 
 * 
FROM 
 finalresults 
ORDER BY 
 finalresults.testtime DESC FETCH FIRST 5 ROWS ONLY;

【问题讨论】:

  • Database Object Naming Rules:带引号的标识符以双引号 (") 开头和结尾。如果您使用带引号的标识符命名架构对象,则无论何时引用该对象都必须使用双引号。
  • finalresults.testtime 应该是 finalresults."testtime",因为您使用的是带引号的标识符(这被认为是不好的做法);或者您可以从查询中删除所有双引号(这被认为是更好的做法)。

标签: sql oracle


【解决方案1】:

您没有发布整个查询;看起来像未完成CTE。

无论如何:我认为罪魁祸首是您将标识符括在双引号中的坏主意。 testtime 列被引用了两次,这看起来很可疑:

SELECT t.place "place",
       t.stationnr "stationnr",
       convres (t.result) "convres",
       t.TYPE "type",
       t.testtime "testtime"             --> here it is
  FROM tests t

正如我所说:它尚未完成,但是 - 关于你到目前为止所说的 - order by 子句可能看起来像这样:

select ... 
from finalresults
order by testtime      --> this

如果您引用的列(或别名)的名称包含在双引号中,则必须这样做总是:

order by "testtime"

在 Oracle 中,这通常是个坏主意。尽可能避免使用双引号(几乎总是这样)。

【讨论】:

    【解决方案2】:

    您的第一个错误是因为您定义了列别名:

    finalresults AS (
     SELECT 
       t.place "place", 
       t.stationnr "stationnr", 
       convres(t.result) "convres", 
       t.type "type", 
       t.testtime "testtime"
    

    然后使用以下方式引用它:

    ORDER BY 
     finalresults.testtime DESC
    

    哪个不使用相同的引用标识符。

    它应该是:

    ORDER BY 
     finalresults."testtime" DESC
    

    但是,您将在定义时遇到下一个错误:

    WITH tests AS (
     SELECT 
       t.place, 
       t.stationnr, 
       t.result, 
       i.type, 
       t.time,
     FROM
       ...
    

    这是无效的语法,因为它有一个尾随逗号;但忽略这一点,然后您继续使用:

    finalresults AS (
     SELECT 
       t.place "place", 
       t.stationnr "stationnr", 
       convres(t.result) "convres", 
       t.type "type", 
       t.testtime "testtime"
     FROM 
       tests t 
       ...
    

    但是您的 TESTS 子查询分解子句从不 SELECTed testtime 列,因此您将收到无效标识符的错误。


    然后你有更多无效的语法:

     GROUP BY  
       t.place, 
       t.stationnr, 
       t.type, 
       t.testtime
       t.result
    

    最后两个标识符之间缺少逗号。


    您可能想要的是摆脱所有子查询分解子句,只使用一个SELECT,类似于:

    SELECT t.place, 
           t.stationnr, 
           convres(t.result) AS convres, 
           i.type, 
           t.testtime -- or should this be t.time?
    FROM   result_test t 
           INNER JOIN result_ident i ON t.place= i.place
           INNER JOIN stat_testplacegroup pg on pg.place = t.place 
           INNER JOIN result_release r ON i.type= r.type
           INNER JOIN result_testecu_11 tecu ON tecu.testresid = t.testresid 
           INNER JOIN result_pathecu pecu ON pecu.ecuid = tecu.ecuid 
           INNER JOIN result_pathfunc func ON pecu.ecuid = func.ecuid 
    WHERE  t.testtime BETWEEN DATE '2022-11-20' AND DATE '2022-11-26'
    GROUP BY  
           t.place, 
           t.stationnr, 
           i.type, 
           t.testtime, -- or should this be t.time?
           t.result
    ORDER BY t.testtime DESC
    FETCH FIRST 5 ROWS ONLY;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 2014-03-02
      • 1970-01-01
      相关资源
      最近更新 更多