如何为下面的查询结果插入列

问题描述 投票:0回答:1
select year, 
 avg(combined_mpg_ft1) as Avg_MPG, 
 avg(tailpipe_co2_in_grams_mile_ft1) as CO2Avg_withoutEV
from fuel$
where fuel_type_1 <> 'Electricity'
group by year
order by year desc

这是我的查询,我想存储 Avg_MPG 和 CO2Avg_withoutEV 的结果以进一步在计算中使用

这是查询结果:

year    Avg_MPG             CO2Avg_withoutEV
2017    23.3069727891156    403.622448979592
2016    23.2543068088597    401.520098441345
2015    22.9001597444089    410.376198083067
2014    22.4621212121212    420.217171717172
2013    22.1125541125541    426.671861471861
2012    21.2674315975287    442.702780760987
2011    20.6821844225604    453.368674056042
2010    20.5172727272727    455.575235248908
2009    19.6935483870968    472.894285955177
2008    19.2099494097808    484.636291784655
2007    18.9786856127886    488.015485242184
2006    18.9592391304348    490.083167976901
2005    19.1938250428816    486.015500057546
2004    19.0677361853832    488.114786450446
2003    18.9443911792905    493.684013045447
2002    19.087358684481     488.743285382219
2001    19.2461368653422    484.083871743709
2000    19.2954545454545    482.456799430981
1999    19.3656804733728    482.362414669942
1998    19.4289245982695    481.056745493943
1997    19.4291338582677    482.244866706037
1996    19.5847347994825    479.560836009444
1995    18.7973112719752    502.192459531232
1994    19.0122199592668    494.883790328922
1993    19.1043000914913    494.837783902195
1992    18.8626226583408    502.228115497502
1991    18.8259717314488    504.270073251502
1990    19.0009276437848    499.800704041559
1989    19.1257588898526    499.143176756376
1988    19.3283185840708    493.083160108052
1987    19.2285485164395    499.584206543624
1986    19.5504132231405    490.45326128694
1985    19.8083480305703    487.146809266722
1984    19.8818737270876    489.237133709112

我想将第二列和第三列的结果存储为表中的新列。

sql
1个回答
0
投票

小提琴

with example as
(
select year, 
 avg(combined_mpg_ft1) as Avg_MPG, 
 avg(tailpipe_co2_in_grams_mile_ft1) as CO2Avg_withoutEV
from fuel$
where fuel_type_1 <> 'Electricity'
group by year
order by year desc
)
    UPDATE fuel$  SET fuel$.Avg_MPG=e.Avg_MPG, fuel$.CO2Avg_withoutEV=e.CO2Avg_withoutEV
    FROM fuel$ f
    INNER JOIN Example e on e.year=f.year
© www.soinside.com 2019 - 2024. All rights reserved.