簡體   English   中英

Oracle無效標識符錯誤

[英]Oracle Invalid Identifier Error

我知道以前曾有人問過這個問題,但我看過其他問題,但我的查詢仍然無法正常工作。 我有一張桌子,上面有過去100年左右的美國職業棒球大聯盟擊球統計數據,我試圖找到該選手的ID,本壘打以及該年(2012)本壘打總命中率的百分比。

查詢:

select playerid, hr, hr/sum(hr) over (partition by playerid, yearid)*100 p
from mlbbattingstats 
where yearid=2012 and p != 0
order by hr;

錯誤:

第3行發生錯誤:
ORA-00904:“ P”:無效的標識符

我嘗試了多種不同的別名,並得到了相同的錯誤。 如果以前已回答過,對我做錯的任何幫助將不勝感激。

您不能在相同的查詢級別上引用列別名(除了order by )。 您需要將語句包裝到派生表中:

select *
from (
  select playerid, hr, hr/sum(hr) over (partition by playerid, yearid)*100 p
  from mlbbattingstats 
  where yearid = 2012 
) 
where p <> 0
order by hr;

如果p <> 0 ,則hr <> 0 因此,您的查詢似乎等同於:

select playerid, hr,
       hr/sum(hr) over (partition by playerid, yearid)*100 as p
from mlbbattingstats 
where yearid = 2012 and hr <> 0
order by hr;

您最初的問題是,不能將where子句中的select中定義的列別名用作同一級別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM