我有一个包含time_id,bus_id,stop_id和time列的表。像这样:
time_id |bus_id | stop_id | time 1 |19 | 51 | 00:08:00 2 |19 | 51 | 00:09:00 3 |19 | 51 | 00:10:00 4 |19 | 12 | 00:08:30 5 |19 | 12 | 00:09:30 6 |19 | 12 | 00:10:30 7 |23 | 31 | 00:08:00 8 |23 | 31 | 00:09:00 9 |23 | 31 | 00:10:00 10 |23 | 42 | 00:08:30 11 |23 | 42 | 00:09:30 12 |23 | 42 | 00:10:30
我想要每组(bus_id和stop_id)的min(时间)行。像这样:
time_id |bus_id | stop_id | time 1 |19 | 51 | 00:08:00 4 |19 | 12 | 00:08:30 7 |23 | 31 | 00:08:00 10 |23 | 42 | 00:08:30
任何帮助都会很棒。使用SQLite。
答案 0 :(得分:2)
执行此操作的标准SQL是:
SELECT bus_id, stop_id, MIN(time) as time
FROM tablename
GROUP BY bus_id, stop_id
ORDER BY bus_id, stop_id
这将返回总线和停止ID的每个组合的时间字段的最小值。我还使用ORDER BY子句对结果进行了排序。