iTurki
iTurki

Reputation: 16398

Retrieve AVG Temp by Month using BigQuery?

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)?

Upvotes: 1

Views: 415

Answers (1)

Mikhail Berlyant
Mikhail Berlyant

Reputation: 172944

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

Upvotes: 5

Related Questions