ORA-00904解码别名上的无效标识符

use*_*778 2 sql oracle alias decode

当我尝试decode在我的select语句中使用a的别名时,我遇到了标题中声明的错误.这是代码:

SELECT DISTINCT rl.complaint_date, 
                  decode(rl.judgement_date,null,rl.complaint_amt,rl.judgement_amt) as account_amt, 
                  rl.date_served1, 
                  rl.date_served2,
                  rl.judgement_date,         
                  rl.skip_locate,
                  rl.case_no,
                  lcc.bal_range_min, 
                  lcc.bal_range_max, 
                  lcc.cost_range_min, 
                  lcc.cost_range_max, 
                  lcc.court,
                  lcc.county AS lcc_county,
                  ah.ACCOUNT, 
                  ah.transaction_code, 
                  ah.transaction_date, 
                  ah.rule_id, 
                  ah.amount, 
                  ah.description,                    
                  r.state, 
                  r.zip_code, 
                  z.county AS ah_county,
                  z.county_2,
                  z.county_3,
                  z.county_4
  FROM legal_address_skip las,
       racctrel r, 
       ziplist z, 
       legal_court_cost lcc, 
       racctlgl rl,
       legal_transaction_review ah
  WHERE ah.ACCOUNT = rl.ACCOUNT
  AND ah.ACCOUNT = las.ACCOUNT(+)
  AND ah.ACCOUNT = r.ACCOUNT
  AND nvl(lpad(substr(r.zip_code,0,instr(r.zip_code,'-')-1),5,0), substr(r.zip_code,1,5)) = z.zip 
  AND r.state = lcc.state
  AND (REPLACE(lcc.county,' ','') = REPLACE(upper(z.county),' ','') 
       OR REPLACE(lcc.county,' ','') = REPLACE(upper(z.county_2),' ','')
       OR REPLACE(lcc.county,' ','') = REPLACE(upper(z.county_3),' ','')
       OR REPLACE(lcc.county,' ','') = REPLACE(upper(z.county_4),' ',''))
  AND lcc.transaction_code = ah.transaction_code
  AND lcc.transaction_code = 1
  AND lcc.end_date IS NULL
  AND ah.amount NOT BETWEEN lcc.cost_range_min AND lcc.cost_range_max
  AND (account_amt NOT BETWEEN lcc.bal_range_min AND lcc.bal_range_max
      OR lcc.bal_range_min - account_amt NOT BETWEEN 0 AND 500)
  ORDER BY CASE 
           WHEN ah.amount NOT BETWEEN lcc.cost_range_min AND lcc.cost_range_max THEN 1
           WHEN ah.amount BETWEEN lcc.cost_range_min AND lcc.cost_range_max THEN 2 END, ah.amount;
Run Code Online (Sandbox Code Playgroud)

我之前在select语句中使用了别名,所以我很困惑为什么我收到错误.在这种情况下它是否有所不同?

Ale*_*ole 6

从文档(重点添加):

您可以使用列别名c_alias来标记选择列表中的前一个表达式,以便使用新标题显示该列.别名在查询期间有效地重命名选择列表项.别名可以在 ORDER BY子句中使用,但不能在查询中使用其他子句.

所以你不能在where子句中引用别名,你现在在哪里:

...
AND (account_amt NOT BETWEEN ...
...
Run Code Online (Sandbox Code Playgroud)

别名在该点无效,因此它在其中一个表中查找具有该名称的列,但未找到一个.order by虽然这很好.

您需要使用重复decode语句替换别名,或者可能使用子查询,然后引用where外部查询中的子句中的别名,但这可能最终会降低效率,具体取决于您的其他条件的选择性.


Yia*_*nes 5

Oracle按照以下顺序运行选择查询:

  1. FROM子句
  2. WHERE子句
  3. GROUP BY子句
  4. HAVING子句
  5. SELECT子句
  6. ORDER BY子句

根据以上内容,您可以看到当您在WHERE部分中时,尚未创建别名。如果要使用SELECT部分​​的结果,可以通过如下修改查询来做到这一点:

WITH q AS 
(
-- Your query without the extra AND
)
SELECT *
FROM q
WHERE --put your check here
Run Code Online (Sandbox Code Playgroud)

这样,当您到达WHERE部分时,您将已经拥有别名。

希望这可以帮助!:)