我正在尝试在 PostgreSQL 9.0 中构建一个查询,该查询获取特定列的最长连续行序列。
考虑下表:
lap_id (serial), lap_no (int), car_type (enum), race_id (int FK)
Run Code Online (Sandbox Code Playgroud)
lap_no每个(race_id, car_type). where都是独一无二的。
我希望查询为给定的race_idand生成最长的序列car_type,因此它将返回int最高的(或长的)。
使用以下数据:
1, 1, red, 1
2, 2, red, 1
3, 3, red, 1
4, 4, red, 1
5, 1, blue, 1
6, 5, red, 1
7, 2, blue, 1
8, 1, green, 1
Run Code Online (Sandbox Code Playgroud)
对于car_type = red and race_id = 1查询将5作为lap_no字段的最长序列返回。
(我也想知道car_type …