【问题标题】:SELECT the min time for each bus stop选择每个巴士站的最短时间
【发布时间】:2023-04-07 14:03:01
【问题描述】:

我有一个包含 time_id、bus_id、stop_id 和 time 列的表。像这样:

time_id |bus_id | stop_id |时间 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)的最小(时间)行。像这样:

time_id |bus_id | stop_id |时间 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。

【问题讨论】:

    标签: sql sqlite time min


    【解决方案1】:

    执行此操作的标准 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 子句对结果进行了排序。

    【讨论】:

    • 是的,它就像一个魅力...... group by 对我来说是新的,这太棒了,非常感谢!
    猜你喜欢
    • 2020-09-06
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多