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 (PARTITIONBY start_station_id ORDERBY 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 (
SELECTARRAY_AGG(t ORDERBY start_time DESC LIMIT 5) arr
FROM `bpd.shop.trips` t
GROUPBY 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 (PARTITIONBY start_station_id ORDERBY 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"