如何在Mysql中将持续时间行分割为一小时段

发布于 2025-01-21 01:49:11 字数 1534 浏览 0 评论 0原文

我有开始时间。从那里,我使用 Lead 函数来获取结束时间并计算持续时间。现在我试图按小时分割持续时间。这是当前的代码和输出:

SELECT `time`, LEAD(time,1,null) OVER (ORDER BY time asc) time_end, 
unix_timestamp(LEAD(time,1,null) OVER ( ORDER BY time asc)) - unix_timestamp(time) as 
duration
FROM test_table

当前输出:

+------+---------------------+---------------------+---------------+
| name |        start        |         end         | (duration)s   |
+------+---------------------+---------------------+---------------+
|  me  | 2017-04-03 23:00:00 | 2017-04-03 23:30:00 |          1800 |
|  me  | 2017-04-03 23:30:00 | 2017-04-04 00:45:00 |          5100 |
|  me  | 2017-04-04 10:00:00 | 2017-04-04 11:00:00 |          3600 |
|  me  | 2017-04-04 11:00:00 | 2017-04-04 11:30:00 |          1800 |
+------+---------------------+---------------------+---------------+

预期输出:

 +------+---------------------+---------------------+---------------+
 | name |        start        |         end         | (duration)s   |
 +------+---------------------+---------------------+---------------+
 |  me  | 2017-04-03 23:00:00 | 2017-04-03 23:30:00 |          1800 |
 |  me  | 2017-04-03 23:30:00 | 2017-04-04 00:00:00 |          1800 |
 |  me  | 2017-04-03 00:00:00 | 2017-04-04 00:45:00 |          2700 |
 |  me  | 2017-04-04 10:00:00 | 2017-04-04 11:00:00 |          3600 |
 |  me  | 2017-04-04 11:00:00 | 2017-04-04 11:30:00 |          1800 |
 +------+---------------------+---------------------+---------------+

I have start time. From there, I used lead function to get the end time and calculate the duration. Now I am trying to split a time duration row by hour. This is the current code and output:

SELECT `time`, LEAD(time,1,null) OVER (ORDER BY time asc) time_end, 
unix_timestamp(LEAD(time,1,null) OVER ( ORDER BY time asc)) - unix_timestamp(time) as 
duration
FROM test_table

Current Output:

+------+---------------------+---------------------+---------------+
| name |        start        |         end         | (duration)s   |
+------+---------------------+---------------------+---------------+
|  me  | 2017-04-03 23:00:00 | 2017-04-03 23:30:00 |          1800 |
|  me  | 2017-04-03 23:30:00 | 2017-04-04 00:45:00 |          5100 |
|  me  | 2017-04-04 10:00:00 | 2017-04-04 11:00:00 |          3600 |
|  me  | 2017-04-04 11:00:00 | 2017-04-04 11:30:00 |          1800 |
+------+---------------------+---------------------+---------------+

Expected Output:

 +------+---------------------+---------------------+---------------+
 | name |        start        |         end         | (duration)s   |
 +------+---------------------+---------------------+---------------+
 |  me  | 2017-04-03 23:00:00 | 2017-04-03 23:30:00 |          1800 |
 |  me  | 2017-04-03 23:30:00 | 2017-04-04 00:00:00 |          1800 |
 |  me  | 2017-04-03 00:00:00 | 2017-04-04 00:45:00 |          2700 |
 |  me  | 2017-04-04 10:00:00 | 2017-04-04 11:00:00 |          3600 |
 |  me  | 2017-04-04 11:00:00 | 2017-04-04 11:30:00 |          1800 |
 +------+---------------------+---------------------+---------------+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文