我正在尝试将两个不同的数据集与Google BigQuery上的子查询结合起来。
当我单独使用子查询时,它会运行并且每年都会返回总平均温度。但是,当我将查询作为子查询合并时,它只返回所述期间的平均温度,而不是每年的平均温度。
我的代码:
#standardSQL
SELECT t1.year, gas_fuel, liquid_fuel, solid_fuel, cement, annual_anomaly, (SELECT avg(temp)
FROM `bigquery-public-data.noaa_gsod.gsod*`
WHERE year > '1982') as average_temperature
FROM `cs779-term-project.CO2_FossilFuel.GlobalEmissions` as t1
JOIN `cs779-term-project.CO2_FossilFuel.SeaLevels` as t2
on t1.year = t2.year
Where t1.year > 1982
ORDER BY t1.year ASC;
此外,当我在子查询中添加GROUP BY year
时,它会显示以下错误:
错误:标量子查询产生了多个元素
谢谢!
答案 0 :(得分:1)
如果您需要一年中的平均温度,那么您需要在子查询条件中使用查询年份:
SELECT t1.year, gas_fuel, liquid_fuel, solid_fuel, cement, annual_anomaly
, (SELECT avg(temp)
FROM `bigquery-public-data.noaa_gsod.gsod*`
WHERE year = t1.year) as average_temperature
FROM `cs779-term-project.CO2_FossilFuel.GlobalEmissions` as t1
JOIN `cs779-term-project.CO2_FossilFuel.SeaLevels` as t2
on t1.year = t2.year
Where t1.year > 1982
ORDER BY t1.year ASC;
答案 1 :(得分:0)
这是我使用和运行的最终代码:
#standardSQL
SELECT t1.year, gas_fuel, liquid_fuel, solid_fuel, cement, annual_anomaly
, (SELECT avg(temp)
FROM `bigquery-public-data.noaa_gsod.gsod*`
WHERE CAST(year as INT64) = t1.year) as average_temperature
FROM `cs779-term-project.CO2_FossilFuel.GlobalEmissions` as t1
JOIN `cs779-term-project.CO2_FossilFuel.SeaLevels` as t2
on t1.year = t2.year
Where t1.year > 1982
ORDER BY t1.year ASC;