我有一张表,上面列出了旅行次数和station_id,我想返回每个ID最近进行的5次旅行(下表的示例图片)
我在下面进行的查询汇总了车站ID和最近的行程,但是我很难返回最近的5个行程
SELECT start_station_id, MAX(start_time)
FROM `bpd.shop.trips`
group by start_station_id, start_time
任何帮助将不胜感激,谢谢!
答案 0 :(得分:2)
您可以使用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;
答案 1 :(得分:0)
以下是用于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作为更具扩展性的选项