每个 ID 返回 5 个最近的行程

Returning 5 Most Recent Trips Per ID

我有一个 table 和一个 station_id,我想 return 每个 ID 的最近 5 次旅行(table在下面)

我在下面进行的查询汇总了车站 ID 和最近的行程,但我遇到了困难return最近的 5 个

SELECT start_station_id, MAX(start_time) 
FROM `bpd.shop.trips` 
group by start_station_id, start_time

行程: https://imgur.com/Ebh9FeZ

非常感谢任何帮助,谢谢!

您可以使用 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;

以下适用于 BigQuery 标准 SQL

选项 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   

选项 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   

我建议使用选项 1 作为更具扩展性的选项