Retrieve AVG Temp by Month using BigQuery?

自古美人都是妖i 提交于 2019-12-13 19:27:49

问题


Using the fh-bigquery:weather_gsod dataset, I want to retrieve some monthly weather data for all stations in a specific country. Namely, I want the monthly avg temp, monthly avg max, and monthly avg min, from 1929 to present.

This what I wrote to retrieve what I need from one table, 2015. The data I get seems correct:

SELECT stn, FIRST(name) AS station_name, mo, (AVG(temp)-32)*0.5556 AS temp, (AVG(max)-32)*0.5556 AS max, (AVG(min)-32)*0.5556 AS min
FROM [fh-bigquery:weather_gsod.gsod2015] gsod
JOIN [fh-bigquery:weather_gsod.stations2] stations
ON gsod.wban=stations.wban AND gsod.stn=stations.usaf
WHERE country='SA' 
GROUP BY stn, mo
ORDER BY mo

Assuming that this query does indeed retrieve the info I need, how can I rewrite it so that I can include the whole range (1929 to 2016)?


回答1:


You should use Table wildcard functions for this as in below

SELECT 
  stn, 
  FIRST(name) AS station_name, 
  mo, 
  (AVG(temp)-32)*0.5556 AS temp, 
  (AVG(max)-32)*0.5556 AS max, 
  (AVG(min)-32)*0.5556 AS min
FROM (
  SELECT * FROM
  (TABLE_QUERY([fh-bigquery:weather_gsod], 'table_id CONTAINS "gsod"')) 
) gsod
JOIN [fh-bigquery:weather_gsod.stations2] stations
ON gsod.wban=stations.wban AND gsod.stn=stations.usaf
WHERE country='SA' 
GROUP BY stn, mo
ORDER BY mo


来源:https://stackoverflow.com/questions/37481635/retrieve-avg-temp-by-month-using-bigquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!