我有一个表,其中有两列。一栏(通话开始)显示了通话开始时间。另一列(通话结束)显示了通话结束时间。
现在,我想在一天之内得出结果,该结果将取决于当时正在运行多少个呼叫的条件。 那个时候已经开始打了多少电话。
表:
+-------------+-------------+------+-----+---------------------+-------+
| 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:在特定时间运行的并发调用。 cps:在特定时间开始的呼叫。
注意:
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 |
--------------------------------------------
任何人都请分享一些建议。
答案 0 :(得分:1)
编辑:我以前不明白这个问题,但是看到预期的结果为我澄清了这个问题。 为了在一个时间窗口内每秒显示一行,您需要创建一个包含所有要显示时间的表。
类似这样的东西:
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
这里是SQLFiddle
希望有帮助