MySQLSelect一段时间内的最大并发调用数
MySQL Select maximum concurrent calls in a time period
我有一个 table 存储所有通过系统的呼叫。
我感兴趣的列只是 calldate 和 callend,它们是通话的开始和结束时间。
我想select同时进行了多少个并发调用。
我有一个查询,其中 return 所有重叠调用,但这并不能完全满足我的要求。
SELECT SEC_TO_TIME(FLOOR((TIME_TO_SEC(t.calldate)+300)/600)*600) AS `TIME`,
MAX(t.overlaps)
FROM(
SELECT x.id,
x.calldate,
COUNT(y.id) overlaps
FROM cdr x
LEFT
JOIN cdr y
ON y.id <> x.id
AND y.calldate < x.callend
AND y.callend > x.calldate
AND y.calldate BETWEEN '2015-05-01 00:00:00' AND '2015-05-01 23:59:59'
WHERE
x.calldate BETWEEN '2015-05-01 00:00:00' AND '2015-05-01 23:59:59'
GROUP BY
x.id
) t
GROUP BY `TIME`;
这是 return 告诉我一些正确的事情,但是因为它 return 所有重叠的调用它实际上可能与在那之前完成的调用重叠所以一些数字太高
Call 08:00 08:05 08:10 08:15 08:20 08:25
1 |-----------------|
2 |-----------------|
3 |--------------|
4 |--------------------------------------|
5 |--------------|
6 |------------------------------------|
1 2 3 4 5 4 3 4 3 2 1 0
例如 return 5 因为它查看的是呼叫 ID 而不是日期,我只是不确定如何更改它才能正确使用日期。
我可能遗漏了一些简单的东西,但这是漫长的一天。
编辑
使用 edTarik 的回答有效:
SELECT y.`Interval`,
@lines := CAST((@lines + y.t) AS INT) as TotayQuantity
FROM
(SELECT @lines := 0) init,
(
SELECT
SEC_TO_TIME(FLOOR((TIME_TO_SEC(x.cd)+300)/600)*600) as `Interval`,
SUM(x.t) as t
FROM
(
SELECT
calldate as cd,
1 as t
FROM cdr c
WHERE
calldate BETWEEN '2015-05-04 00:00:00' AND '2015-05-04 23:59:59'
UNION ALL
SELECT
callend as cd,
-1 as t
FROM cdr c
WHERE
callend BETWEEN '2015-05-04 00:00:00' AND '2015-05-04 23:59:59'
ORDER BY cd
) x
GROUP BY `Interval`
) y
您需要执行以下操作:
Select calldate, 1 From cdr
Union
Select callend, -1 from cdr
Order by calldate
然后你 select 累加和然后找到最大值。参见 How to get running sum of a column in sql server
我有一个 table 存储所有通过系统的呼叫。
我感兴趣的列只是 calldate 和 callend,它们是通话的开始和结束时间。
我想select同时进行了多少个并发调用。
我有一个查询,其中 return 所有重叠调用,但这并不能完全满足我的要求。
SELECT SEC_TO_TIME(FLOOR((TIME_TO_SEC(t.calldate)+300)/600)*600) AS `TIME`,
MAX(t.overlaps)
FROM(
SELECT x.id,
x.calldate,
COUNT(y.id) overlaps
FROM cdr x
LEFT
JOIN cdr y
ON y.id <> x.id
AND y.calldate < x.callend
AND y.callend > x.calldate
AND y.calldate BETWEEN '2015-05-01 00:00:00' AND '2015-05-01 23:59:59'
WHERE
x.calldate BETWEEN '2015-05-01 00:00:00' AND '2015-05-01 23:59:59'
GROUP BY
x.id
) t
GROUP BY `TIME`;
这是 return 告诉我一些正确的事情,但是因为它 return 所有重叠的调用它实际上可能与在那之前完成的调用重叠所以一些数字太高
Call 08:00 08:05 08:10 08:15 08:20 08:25
1 |-----------------|
2 |-----------------|
3 |--------------|
4 |--------------------------------------|
5 |--------------|
6 |------------------------------------|
1 2 3 4 5 4 3 4 3 2 1 0
例如 return 5 因为它查看的是呼叫 ID 而不是日期,我只是不确定如何更改它才能正确使用日期。
我可能遗漏了一些简单的东西,但这是漫长的一天。
编辑
使用 edTarik 的回答有效:
SELECT y.`Interval`,
@lines := CAST((@lines + y.t) AS INT) as TotayQuantity
FROM
(SELECT @lines := 0) init,
(
SELECT
SEC_TO_TIME(FLOOR((TIME_TO_SEC(x.cd)+300)/600)*600) as `Interval`,
SUM(x.t) as t
FROM
(
SELECT
calldate as cd,
1 as t
FROM cdr c
WHERE
calldate BETWEEN '2015-05-04 00:00:00' AND '2015-05-04 23:59:59'
UNION ALL
SELECT
callend as cd,
-1 as t
FROM cdr c
WHERE
callend BETWEEN '2015-05-04 00:00:00' AND '2015-05-04 23:59:59'
ORDER BY cd
) x
GROUP BY `Interval`
) y
您需要执行以下操作:
Select calldate, 1 From cdr
Union
Select callend, -1 from cdr
Order by calldate
然后你 select 累加和然后找到最大值。参见 How to get running sum of a column in sql server