SQL-根据时间戳记录查找结束日期和开始日期
目前,我有下表,我想将其汇总到每个资产_ID状态更改的日志中,其中包括开始和结束日期。
即,如果这是我的数据:
asset_id | status_id | update_date |
---|---|---|
1 | 1 | 2021-06-29 01:00:00 |
1 | 1 | 2021-06-29 04:00:00 |
1 | 3 | 2021-06-29 056-29 05:00:00 |
1 | 3 | 2021 05:00:31 |
1 | 1 | 2021-06-29 05:01:00 |
1 | 1 | 2021-06-29 05:08:00 |
1 | 2 | 2021-06-06-06-06-06-06-30 12:12:12 |
12 | -06-29 | 2021-06 -30 12:15:12 |
1 | 2 | 2021-07-30 04:12:12 |
我想将其汇总到:
asset_id | status_id state_id | start_date | end_date end_date |
---|---|---|---|
1 | 1 | 2021-06-29 01:00:00 | 2021-06-29 04: 59:59 |
1 | 3 | 2021-06-29 05:00:00 | 2021-06-29 05:00:59 |
1 | 1 | 2021-06-29 05:01:00 | 2021-06-06-06-30 12:12:11 |
1 | 2 | 2021 -06-30 12:12:12 | 2099-12-31 00:00:0 |
我找到了类似的建议解决方案,但是它们都不包括实际的状态_ID,而end_date直到下一个行开始时间才包括时间减去第二或分钟,即获取列表从数据表的开始和结束值
Currently I have the following table, and I want to aggregate it to some kind of a log of the status changes for each asset_id, that will include a start and end date.
ie, If this is my data:
Asset_Id | Status_Id | Update_Date |
---|---|---|
1 | 1 | 2021-06-29 01:00:00 |
1 | 1 | 2021-06-29 04:00:00 |
1 | 3 | 2021-06-29 05:00:00 |
1 | 3 | 2021-06-29 05:00:31 |
1 | 1 | 2021-06-29 05:01:00 |
1 | 1 | 2021-06-29 05:08:00 |
1 | 2 | 2021-06-30 12:12:12 |
1 | 2 | 2021-06-30 12:15:12 |
1 | 2 | 2021-07-30 04:12:12 |
I would like to aggregate it to:
Asset_Id | Status_Id | Start_date | End_Date |
---|---|---|---|
1 | 1 | 2021-06-29 01:00:00 | 2021-06-29 04:59:59 |
1 | 3 | 2021-06-29 05:00:00 | 2021-06-29 05:00:59 |
1 | 1 | 2021-06-29 05:01:00 | 2021-06-30 12:12:11 |
1 | 2 | 2021-06-30 12:12:12 | 2099-12-31 00:00:0 |
I've found similar proposed solutions, but none of them included the actual status_id and the end_date didn't included the time until the next row start time minus a second or minute, ie Get list with start and end values from table of datetimes
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试以下解决方案。
sql
sql#2
满足以下内容
输出
输出#2
Please try the following solution.
SQL
SQL #2
To satisfy the following
Output
Output #2
我遵循了Larnu在OP评论中的建议,并将SQL脚本调整到下面,这是我所需要的
I've followed Larnu recommendation on the op comment and adjusted the SQL script to the below, this is provided me what I need