Skip to content Skip to sidebar Skip to footer

Returning 5 Most Recent Trips Per ID

I have a table with the number of trips taken and a station_id, and I want to return the 5 most recent trips made per ID (sample image of the table is below) The query I made below

Solution 1:

You can use row_number():

SELECT t.*
FROM (SELECT t.*,
              ROW_NUMBER() OVER (PARTITION BY start_station_id ORDER BY start_time DESC) as seqnum
      FROM `bpd.shop.trips` t
     ) t
WHERE seqnum <= 5;

Solution 2:

Below is for BigQuery Standard SQL

Option 1

#standardSQL
SELECT record.*
FROM (
  SELECT ARRAY_AGG(t ORDER BY start_time DESC LIMIT 5) arr
  FROM `bpd.shop.trips` t
  GROUP BY start_station_id
), UNNEST(arr) record   

Option 2

#standardSQL
SELECT * EXCEPT (pos) FROM (
  SELECT *, ROW_NUMBER() OVER(win) AS pos
  FROM `bpd.shop.trips`
  WINDOW win AS (PARTITION BY start_station_id ORDER BY start_time DESC)
) 
WHERE pos <= 5   

I recommend using Option 1 as more scalable option


Post a Comment for "Returning 5 Most Recent Trips Per ID"