2

我需要帮助,我需要计算当前的活动呼叫数。这是我的桌子:

+---------------------+---------+
| calldate            | billsec |
+---------------------+---------+
| 2013-05-14 09:40:30 |      29 |
| 2013-05-14 09:40:31 |      24 |
| 2013-05-14 09:40:31 |      30 |
| 2013-05-14 09:40:33 |      20 |
| 2013-05-14 09:40:34 |      21 |
| 2013-05-14 09:40:35 |      30 |
| 2013-05-14 09:40:36 |      30 |
| 2013-05-14 09:40:37 |      30 |
| 2013-05-14 09:40:37 |      25 |
| 2013-05-14 09:40:39 |      22 |
| 2013-05-14 09:40:39 |      19 |
| 2013-05-14 09:40:40 |      22 |
| 2013-05-14 09:40:41 |      22 |

例如,如果我想计算 '2013-05-14 09:40:34' 中的活动通话次数,我应该得到 5 条记录,因为例如,第一行通话在 '2013-05-14 09 之间:40:30' 和 '2013-05-14 09:40:59' (calldate+billsec)。

谢谢

4

1 回答 1

2
select count(*) from calls 
where '2013-05-14 09:40:34' between
  calldate and DATE_ADD(calldate,INTERVAL billsec SECOND)  

SQLFIDDLE

更新:
为了查看每个调用日期的金额,您可以使用子查询

select B.calldate
  ,(select count(*) from calls A
  where B.calldate between
    A.calldate and DATE_ADD(A.calldate,INTERVAL A.billsec SECOND)) numcalls
from calls B
group by B.calldate 

SQLFIDDLE

于 2013-07-04T08:14:30.233 回答