2016-07-07 7 views
1

私のデータベースから平均を1年間得ることができます。毎回手動で日付を入力することなく、データベース内のすべての年(2017-2054)のデータを効果的に取得する方法はありますか?それを新しいテーブルに入れるために、すべての日付から平均を選択した後に可能ですか?ありがとう!平均値を計算する方法psql

lcoe=# select techindex, avg(lcoe) from lcoe where year = 2017 group by techindex; 
     techindex  |  avg   
------------------------+-------------------- 
Combustion Turbine  | 0.0564000003039837 
Unscrubbed    | 0.0689999982714653 
Offshore    | 0.119428569717067 
Photovoltaic   | 0.208888891670439 
EGS     | 0.0549999997019768 
Fuel Cell    | 0.115666666378578 
Onshore    | 0.0587692308024718 
Scrubbed    | 0.0556000009179115 
Solar Thermal   | 0.134285714477301 
Combined Cycle   | 0.0457142852246761 
Hydrothermal   | 0.104249998694286 
Hydroelectric   | 0.0765999995172024 
IGCC     | 0.0762727270749482 
Distributed Generation | 0.282000001519918 
Nuclear    | 0.0755000002682209 
Biopower    | 0.125571429197277 
(16 rows)` 
+0

おそらく年だけをselect句とgroup句に追加する必要があります – Leo

答えて

0

あなたは年とハイテクインデックスのすべての組み合わせを生成し、組み合わせごとに平均を生成することができます:あなたが唯一の各年であるtechindexをしたい場合は

select y.year, ti.techindex, avg(lcoe.lcoe) 
from (select distinct year from lcoe) y cross join 
    (select distinct techindex from lco) ti left join 
    lcoe 
    on y.year = lcoe.year and ti.techindex = lcoe.techindex 
group by y.year, ti.techindex; 

を:

select yti.year, yti.techindex, avg(lcoe.lcoe) 
from (select distinct year, techindex from lcoe) yti 
    lcoe 
    on yti.year = lcoe.year and yti.techindex = lcoe.techindex 
group by yti.year, yti.techindex; 
関連する問題