2016-05-09 144 views
0

我只是不叩頭如何使它工作,我如何可以選擇從其他選擇選擇平均FROM選擇平均

select avg(avg(valoare)) from (select avg(valoare) from note 
where nr_matricol=111 group by ID_CURS) 

我也試過

select avg(alias) from (select avg(valoare) as "alias" from note 
    where nr_matricol=111 group by ID_CURS) 
+0

添加一個字母'了'在年底,也將努力 – cha

回答

1

第二個查詢的平均基本上就是你想要的。但是,一旦您開始在SQL中使用雙引號標識符,這些標識符就會區分大小寫:"alias"是與alias不同的名稱(因爲aliasALIAS相同)。

所以你需要在整個查詢中使用雙引號:

select avg("alias") 
from (
    select avg(valoare) as "alias" 
    from note 
    where nr_matricol=111 group by ID_CURS 
) 

另一種選擇是使用不需要引用一個名字:

select avg(avg_valoare) 
from (
    select avg(valoare) as avg_valoare 
    from note 
    where nr_matricol=111 group by ID_CURS 
) 

雖然不是必需的Oracle爲派生表提供別名也是很好的編碼風格。

select avg("alias") 
from (
    select avg(valoare) as "alias" 
    from note 
    where nr_matricol=111 group by ID_CURS 
) x --<<< here 

注意,Oracle不會支持AS關鍵字爲表的別名,所以你不能派生表的別名使用) as x

+2

由於它是甲骨文你可以簡單地窩聚集:-)'選擇AVG(AVG(valoare))從注意 其中nr_matricol = 111組由ID_CURS'(當然,我實際上永遠不會推薦這個) – dnoeth

+0

@dnoeth:一個權利。我總是忘記這一點。 –

1

您可以使用下面的查詢。

select avg(alias) from 
     (select avg(valoare) as alias from note 
     where nr_matricol=111 group by ID_CURS) X 

OR

select avg(avg(valoare)) as alias from note 
    where nr_matricol=111 group by ID_CURS X