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'用于获取下一个日期。