微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MySQL查询列值更改的时间戳

如何解决MySQL查询列值更改的时间戳

当给定ID的特定列值更改时,如何查询日期?下表显示了运行作业以标识oppid的状态的日期。我想跟踪oppid具有status = status_1的时间,以显示设置状态时的最早日期和状态为 status_1时的日期。

| oppid  |    date   |  status   |
+--------+  ---------+-----------+
| 1000   | 2020-07-01|  status_1 |
| 1000   | 2020-07-02|  status_1 |
| 1000   | 2020-07-03|  status_1 |
| 1000   | 2020-07-04|  status_2 |
| 1000   | 2020-07-07|  status_2 |
| 1000   | 2020-07-15|  status_1 |
| 1000   | 2020-07-16|  status_1 |
| 1001   | 2020-07-10|  status_1 |
| 1001   | 2020-07-11|  status_1 |
| 1000   | 2020-08-01|  status_2 |

所需的结果如下所示:

| oppid  |   status  |  status_set | status_changed |
+--------+  ---------+-------------+----------------+
| 1000   | status_1  |  2020-07-01 |   2020-07-04   |
| 1000   | status_1  |  2020-07-15 |   2020-08-01   |
| 1001   | status_1  |  2020-07-10 |                |
 

解决方法

您可以使用窗口功能:

select oppid,status,date as status_set,status_changed
from (select t.*,lag(status) over (partition by oppid order by date) as prev_status,min(case when status <> 'status_1' then date end) over (partition by oppid order by date desc) as status_changed
      from t
     ) t
where (prev_status is null or prev_status <> status) and
      status = 'status_1';

以前的状态用于过滤以查找状态每次变为'status_1'时的情况。如果状态不是min(),则'status_1'用于获取下一个日期。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。