【问题标题】:mySQL query to find first event on a series of events by typemySQL 查询以按类型查找一系列事件的第一个事件
【发布时间】:2021-08-20 16:45:05
【问题描述】:

我在 mySQL 中有一个事件表,它存储可以以successfailed 状态结束的事件。

我希望找到最后一个失败事件系列中的第一个失败。

例如:

 id  time     status
------------------
  1  10:00   success
  2  10:01   success
  3  10:03   failed
  4  10:04   failed
  5  10:05   success
  6  10:06   success
  8  10:07   failed
  9  10:07   failed
  10 10:07   failed

我需要一个返回8 10:07 failed的查询

最后一个失败事件系列的第一个失败

可能需要使用我不太擅长的窗口函数。

有人可以帮忙吗?

非常感谢。

【问题讨论】:

    标签: mysql window-functions


    【解决方案1】:

    我确信还有另一种方法可以做到这一点,但我经常使用内部查询来做到这一点,并使用 min group by。

    select
        d.*
    from data d
    inner join (
        select 
            min(id) id, 
            time 
        from data 
        group by time
    ) l on d.id = l.id
    

    Here's aSQL Fiddle 这个在实践中

    【讨论】:

      猜你喜欢
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 2019-01-27
      • 2020-06-20
      • 2020-09-22
      • 1970-01-01
      相关资源
      最近更新 更多