使用BigQuery按月检索AVG Temp?

时间:2016-05-27 10:54:49

标签: sql google-bigquery weather

使用fh-bigquery:weather_gsod数据集,我想检索特定国家/地区所有电台的月度天气数据。也就是说,我想要从1929年到现在的每月平均温度,每月平均最大值和每月平均最低值。

这是我写的从2015年的一个表中检索我需要的东西。我得到的数据似乎是正确的:

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

假设这个查询确实检索了我需要的信息,我怎样才能重写它以便我可以包含整个范围(1929年至2016年)?

1 个答案:

答案 0 :(得分:2)

您应该使用Table wildcard functions,如下所示

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