从开始和结束时间获取行数

zzlelutf  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(362)

我有一个表,其中有两列。一列(callstart)显示调用开始时间。显示调用结束时间的另一列(callend)。
现在我想做一个单日的结果,这将是在那里的条件,如有多少个电话运行的时间。在那个时间已经开始了多少个电话。
表格:

+-------------+-------------+------+-----+---------------------+-------+
| Field       | Type        | Null | Key | Default             | Extra |
+-------------+-------------+------+-----+---------------------+-------+
| id          | int(11)     | NO   |     | NULL                |       |
| user_id     | int(11)     | YES  |     | 0                   |       |
| callstart   | datetime    | NO   |     | 0000-00-00 00:00:00 |       |
| callend     | datetime    | NO   |     | 0000-00-00 00:00:00 |       |
+-------------+-------------+------+-----+---------------------+-------+
4 rows in set (0.00 sec)

预期结果:

+----+-----------+---------------------+----+-----+
| id | user_id   | date                | cc | cps |
+----+-----------+---------------------+----+-----+
|  1 |         3 | 2018-04-02 19:47:27 |  0 |   1 |
|  2 |         3 | 2018-04-02 19:48:52 |  1 |   1 |
|  3 |         3 | 2018-04-02 19:48:53 |  1 |   0 |
|  4 |         3 | 2018-04-02 19:48:54 |  1 |   0 |
|  5 |         3 | 2018-04-02 19:48:55 |  1 |   0 |
+----+-----------+---------------------+----+-----+

在上述结果中:
cc:在特定时间运行的并发调用。在特定时间开始的呼叫。
注:

1. mysql version is 5.5.

  2. In above table,Data will be available only for single day( A call which has been ended on day upto call has been started on a day).

  3.I have make it working with php script.But its take long time to manage 6 months record.So i would like to make query which can gives result for every single day.

小提琴:http://sqlfiddle.com/#!9/4c042/5年
准时期望结果:2018-08-20 12:20:34需要显示两行

accountid | callstart           | cc |cps|
--------------------------------------------
56        | 2018-08-20 12:20:24 | 1  | 1 |
56        | 2018-08-20 12:20:25 | 1  | 0 |
56        | 2018-08-20 12:20:26 | 1  | 0 |
56        | 2018-08-20 12:20:27 | 1  | 0 |
56        | 2018-08-20 12:20:28 | 1  | 0 |
56        | 2018-08-20 12:20:29 | 1  | 0 |
56        | 2018-08-20 12:20:30 | 1  | 0 |
56        | 2018-08-20 12:20:31 | 1  | 0 |
56        | 2018-08-20 12:20:32 | 1  | 0 |
56        | 2018-08-20 12:20:33 | 1  | 0 |
56        | 2018-08-20 12:20:34 | 3  | 2 |
56        | 2018-08-20 12:20:35 | 3  | 0 |
56        | 2018-08-20 12:20:36 | 3  | 0 |
56        | 2018-08-20 12:20:37 | 3  | 0 |
56        | 2018-08-20 12:20:38 | 3  | 0 |
56        | 2018-08-20 12:20:39 | 3  | 0 |
56        | 2018-08-20 12:20:40 | 3  | 0 |
56        | 2018-08-20 12:20:41 | 2  | 0 |
56        | 2018-08-20 12:20:42 | 2  | 0 |
56        | 2018-08-20 12:20:43 | 2  | 0 |
56        | 2018-08-20 12:20:44 | 2  | 0 |
56        | 2018-08-20 12:20:45 | 2  | 0 |
56        | 2018-08-20 12:20:46 | 2  | 0 |
56        | 2018-08-20 12:20:47 | 2  | 0 |
56        | 2018-08-20 12:20:48 | 2  | 0 |
56        | 2018-08-20 12:20:49 | 2  | 0 |
56        | 2018-08-20 12:20:50 | 0  | 0 |
58        | 2018-08-20 12:20:29 | 1  | 1 |
58        | 2018-08-20 12:20:30 | 1  | 0 |
58        | 2018-08-20 12:20:31 | 1  | 0 |
58        | 2018-08-20 12:20:32 | 1  | 0 |
58        | 2018-08-20 12:20:33 | 1  | 0 |
58        | 2018-08-20 12:20:34 | 3  | 2 |
58        | 2018-08-20 12:20:35 | 3  | 0 |
58        | 2018-08-20 12:20:36 | 3  | 0 |
58        | 2018-08-20 12:20:37 | 3  | 0 |
58        | 2018-08-20 12:20:38 | 3  | 0 |
58        | 2018-08-20 12:20:39 | 3  | 0 |
58        | 2018-08-20 12:20:40 | 3  | 0 |
58        | 2018-08-20 12:20:41 | 3  | 0 |
58        | 2018-08-20 12:20:42 | 3  | 0 |
58        | 2018-08-20 12:20:43 | 3  | 0 |
58        | 2018-08-20 12:20:44 | 3  | 0 |
58        | 2018-08-20 12:20:45 | 3  | 0 |
58        | 2018-08-20 12:20:46 | 3  | 0 |
58        | 2018-08-20 12:20:47 | 3  | 0 |
58        | 2018-08-20 12:20:48 | 3  | 0 |
58        | 2018-08-20 12:20:49 | 2  | 0 |
58        | 2018-08-20 12:20:50 | 2  | 0 |
58        | 2018-08-20 12:20:51 | 2  | 0 |
58        | 2018-08-20 12:20:52 | 2  | 0 |
58        | 2018-08-20 12:20:53 | 2  | 0 |
58        | 2018-08-20 12:20:54 | 2  | 0 |
58        | 2018-08-20 12:20:55 | 2  | 0 |
58        | 2018-08-20 12:20:56 | 2  | 0 |
--------------------------------------------

任何人请分享一些建议。

qvtsj1bj

qvtsj1bj1#

编辑:我以前不理解这个问题,但看到预期的结果让我明白了这一点。为了在时间窗口中每秒钟有一行,您需要创建一个包含所有要显示的时间的表。
像这样:

CREATE TABLE `time_window` (
  `second` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `time_window` (`second`) VALUES
('2018-08-20 00:00:00'),
('2018-08-20 00:00:01'),
 ...
('2018-08-20 23:59:59');

然后,需要运行以下查询:

SELECT 
  b.accountid,
  a.second,
  SUM(CASE WHEN b.callstart <= a.second
      AND b.callend >= a.second THEN 1 ELSE 0 END) AS cc,
  SUM(CASE WHEN b.callstart = a.second THEN 1 ELSE 0 END) AS cps
FROM time_window a
LEFT JOIN custom_data b 
ON 1=1
GROUP BY 1 ,2
ORDER BY b.accountid, a.second

这是小提琴
希望有帮助

相关问题