gpt4 book ai didi

mysql - 根据 time_stamp 计算表中 2 个时间戳之间的行数

转载 作者:行者123 更新时间:2023-11-29 05:49:42 25 4
gpt4 key购买 nike

我有一个表,其中有 2 列:start_timeend_time

它用于电话系统,每次调用开始时都会插入一个新行,其中包含 start_time 并在调用结束时更新 end_time

我想为每个 start_time 计算当时有多少通话,

表格示例:

+------+---------------------+---------------------+
| id | start_time | end_time |
+------+---------------------+---------------------+
| 3052 | 2019-03-28 21:40:25 | 2019-03-28 21:42:41 |
| 3053 | 2019-03-28 21:42:26 | 2019-03-28 21:42:59 |
| 3054 | 2019-03-28 21:42:40 | 2019-03-28 21:44:41 |
| 3055 | 2019-03-28 21:45:24 | 2019-03-28 21:45:55 |
| 3056 | 2019-03-28 21:45:26 | 2019-03-28 21:45:42 |
| 3057 | 2019-03-28 21:46:57 | 2019-03-28 21:48:52 |
| 3058 | 2019-03-28 21:47:11 | 2019-03-28 21:47:37 |
| 3059 | 2019-03-28 21:49:12 | 2019-03-28 21:52:19 |
| 3060 | 2019-03-28 21:49:34 | 2019-03-28 21:53:48 |
| 3061 | 2019-03-28 21:50:51 | 2019-03-28 21:51:33 |
| 3062 | 2019-03-28 21:53:51 | 2019-03-28 21:54:07 |
| 3063 | 2019-03-28 21:54:36 | 2019-03-28 21:55:26 |
| 3064 | 2019-03-28 21:55:34 | 2019-03-28 21:56:16 |
| 3065 | 2019-03-28 21:56:05 | 2019-03-28 21:57:28 |
| 3066 | 2019-03-28 21:56:24 | 2019-03-28 21:56:40 |
| 3067 | 2019-03-28 21:58:48 | 2019-03-28 21:59:16 |
+------+---------------------+---------------------+

我想得到这样的结果:

+------+---------------------+---------------------+-------+
| id | start_time | end_time | count |
+------+---------------------+---------------------+-------+
| 3052 | 2019-03-28 21:40:25 | 2019-03-28 21:42:41 | 1 |
| 3053 | 2019-03-28 21:42:26 | 2019-03-28 21:42:59 | 2 |
| 3054 | 2019-03-28 21:42:40 | 2019-03-28 21:44:41 | 3 |
| 3055 | 2019-03-28 21:45:24 | 2019-03-28 21:45:55 | 1 |
| 3056 | 2019-03-28 21:45:26 | 2019-03-28 21:45:42 | 2 |
| 3057 | 2019-03-28 21:46:57 | 2019-03-28 21:48:52 | 1 |
| 3058 | 2019-03-28 21:47:11 | 2019-03-28 21:47:37 | 2 |
| 3059 | 2019-03-28 21:49:12 | 2019-03-28 21:52:19 | 1 |
| 3060 | 2019-03-28 21:49:34 | 2019-03-28 21:53:48 | 2 |
| 3061 | 2019-03-28 21:50:51 | 2019-03-28 21:51:33 | 3 |
| 3062 | 2019-03-28 21:53:51 | 2019-03-28 21:54:07 | 1 |
| 3063 | 2019-03-28 21:54:36 | 2019-03-28 21:55:26 | 1 |
| 3064 | 2019-03-28 21:55:34 | 2019-03-28 21:56:16 | 1 |
| 3065 | 2019-03-28 21:56:05 | 2019-03-28 21:57:28 | 2 |
| 3066 | 2019-03-28 21:56:24 | 2019-03-28 21:56:40 | 2 |
| 3067 | 2019-03-28 21:58:48 | 2019-03-28 21:59:16 | 1 |
+------+---------------------+---------------------+-------+

对于每个 start_time 同时调用的次数,所以我会知道我正在使用多少 channel 。

最佳答案

您可以自行加入表并使用聚合:

SELECT 
t.id,
t.start_time,
t.end_time,
COUNT(*) cnt
FROM mytable t
LEFT JOIN mytable t1
ON t1.start_time <= t.start_time AND t1.end_time >= t.start_time
GROUP BY
t.id,
t.start_time,
t.end_time
ORDER BY
t.id,
t.start_time

Demo on DB Fiddle :

| id   | start_time          | end_time            | cnt |
| ---- | ------------------- | ------------------- | --- |
| 3052 | 2019-03-28 21:40:25 | 2019-03-28 21:42:41 | 1 |
| 3053 | 2019-03-28 21:42:26 | 2019-03-28 21:42:59 | 2 |
| 3054 | 2019-03-28 21:42:40 | 2019-03-28 21:44:41 | 3 |
| 3055 | 2019-03-28 21:45:24 | 2019-03-28 21:45:55 | 1 |
| 3056 | 2019-03-28 21:45:26 | 2019-03-28 21:45:42 | 2 |
| 3057 | 2019-03-28 21:46:57 | 2019-03-28 21:48:52 | 1 |
| 3058 | 2019-03-28 21:47:11 | 2019-03-28 21:47:37 | 2 |
| 3059 | 2019-03-28 21:49:12 | 2019-03-28 21:52:19 | 1 |
| 3060 | 2019-03-28 21:49:34 | 2019-03-28 21:53:48 | 2 |
| 3061 | 2019-03-28 21:50:51 | 2019-03-28 21:51:33 | 3 |
| 3062 | 2019-03-28 21:53:51 | 2019-03-28 21:54:07 | 1 |
| 3063 | 2019-03-28 21:54:36 | 2019-03-28 21:55:26 | 1 |
| 3064 | 2019-03-28 21:55:34 | 2019-03-28 21:56:16 | 1 |
| 3065 | 2019-03-28 21:56:05 | 2019-03-28 21:57:28 | 2 |
| 3066 | 2019-03-28 21:56:24 | 2019-03-28 21:56:40 | 2 |
| 3067 | 2019-03-28 21:58:48 | 2019-03-28 21:59:16 | 1 |

关于mysql - 根据 time_stamp 计算表中 2 个时间戳之间的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55526097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com