如何在Mysql中将持续时间行分割为一小时段
我有开始时间。从那里,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论