SQL - BigQuery - Optimized Query
- Published on
- • 1 mins read•––– views
SELECT MIN(start_station_name) AS start_station_name, MIN(end_station_name) AS end_station_name, APPROX_QUANTILES(tripduration, 10)[OFFSET (5)] AS typical_duration, COUNT(tripduration) AS num_tripsFROM `bigquery-public-data.new_york_citibike.citibike_trips`WHERE start_station_id != end_station_idGROUP BY start_station_id, end_station_idORDER BY num_trips DESCLIMIT 10
WITH trip_distance AS (SELECT bikeid, ST_Distance(ST_GeogPoint(s.longitude, s.latitude), ST_GeogPoint(e.longitude, e.latitude)) AS distanceFROM `bigquery-public-data.new_york_citibike.citibike_trips`, `bigquery-public-data.new_york_citibike.citibike_stations` as s, `bigquery-public-data.new_york_citibike.citibike_stations` as eWHERE start_station_name = s.name AND end_station_name = e.name)SELECT bikeid, SUM(distance)/1000 AS total_distanceFROM trip_distanceGROUP BY bikeidORDER BY total_distance DESCLIMIT 5
SELECT wx.date, wx.value/10.0 AS prcpFROM `bigquery-public-data.ghcn_d.ghcnd_2015` AS wxWHERE id = 'USW00094728' AND qflag IS NULL AND element = 'PRCP'ORDER BY wx.date
WITH bicycle_rentals AS ( SELECT COUNT(starttime) as num_trips, EXTRACT(DATE from starttime) as trip_date FROM `bigquery-public-data.new_york_citibike.citibike_trips` GROUP BY trip_date),
rainy_days AS(SELECT date, (MAX(prcp) > 5) AS rainyFROM ( SELECT wx.date AS date, IF (wx.element = 'PRCP', wx.value/10, NULL) AS prcp FROM `bigquery-public-data.ghcn_d.ghcnd_2015` AS wx WHERE wx.id = 'USW00094728')GROUP BY date)
SELECT ROUND(AVG(bk.num_trips)) AS num_trips, wx.rainyFROM bicycle_rentals AS bkJOIN rainy_days AS wxON wx.date = bk.trip_dateGROUP BY wx.rainy