我必须查询一个calls.csv表(下面)来提取总呼叫,总通话秒数和平均呼叫秒数:
Callid Qid Tm_init Tm_disc
780164900 24710 1422835548 1422835559
780164901 24710 1422835562 1422835687
我到达了以下查询,但是,我在撰写有关“按天”和“2015年8月期间排队24710”的查询时遇到了一些困难:
SELECT
(AVG(`Tm_disc` - `Tm_init`)) AS AVARAGE_CALLS_SECONDS,
(SUM(`Tm_disc` - `Tm_init`)) AS TOTAL_SECONDS_CALLS,
COUNT(`Callid`) AS TOTAL_NUMBER_CALLS
FROM
calls_database.`calls`
请帮忙吗?
谢谢
答案 0 :(得分:2)
您可以使用FROM_UNIXTIME()将unix日期转换为时间戳,然后使用常规日期时间函数(如DATE()函数)从unixtime中提取日期。
SELECT
(AVG(`Tm_disc` - `Tm_init`)) AS AVARAGE_CALLS_SECONDS,
(SUM(`Tm_disc` - `Tm_init`)) AS TOTAL_SECONDS_CALLS,
COUNT(`Callid`) AS TOTAL_NUMBER_CALLS
FROM
calls
WHERE
Qid=24710
AND
MONTH(FROM_UNIXTIME(Tm_init))=2
AND
YEAR(FROM_UNIXTIME(Tm_init))=2015
GROUP BY DATE(FROM_UNIXTIME(Tm_init))
SQLFiddle:http://sqlfiddle.com/#!9/58dfcc8/9
然后添加过滤器,例如Qid = 1234。