尝试decode在select语句中使用a的别名时,我遇到了标题中所述的错误。这是代码:
decode
select
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;
我之前在select语句中使用过别名,因此我对为什么会收到此错误感到困惑。在这种情况下,它的工作方式是否有所不同?
在文档中(添加了重点):
您可以使用列别名c_alias来标记选择列表中紧接的前一个表达式,以便以新标题显示该列。别名在查询期间有效地重命名了选择列表项。 别名可以在ORDER BY子句中使用,但不能在查询中的其他子句中使用。
c_alias
ORDER BY
因此,您无法在where子句中引用别名,该子句现在位于:
where
... AND (account_amt NOT BETWEEN ... ...
别名当时无效,因此它正在其中一个表中查找具有该名称的列,但未找到该列。order by虽然很好。
order by
您可能需要用重复的decode语句替换别名,或者可能使用子查询,然后where在外部查询的子句中引用别名,但这最终可能会降低效率,具体取决于您对其他条件的选择程度。