以下存储过程会计算通话数据表中特定日期内按小时分组的通话次数和通话时间。
它有以下输出:
但是,我仍然需要将这些结果分组为另外两个字段(数字类型,从00,01 / 02,084等开始):innumber
和outnumber
。因此,所需的输出示例应为:
这是存储过程:
CREATE DEFINER=`xxx`@`%` PROCEDURE `GetHourStats`(IN _ID INT, IN _CALLTYPE INT, IN _YEAR INT, IN _MONTH INT, IN _DAY INT)
BEGIN
set @_START = UNIX_TIMESTAMP(date(_YEAR * 10000 + _MONTH * 100 + _DAY * 1));
set @_END = UNIX_TIMESTAMP(date_add(date(_YEAR * 10000 + _MONTH * 100 + _DAY * 1), interval 1 day));
SELECT *,
(
SELECT COUNT(*)
FROM (SELECT * FROM (
SELECT
`cdrs`.`stype` AS `stype`,
`cdrs`.`dtype` AS `dtype`,
`cdrs`.`uniqueid` AS `UniqueID`,
`cdrs`.`dcustomer` AS `dcustomer`,
`cdrs`.`callid` AS `callid`,
(CASE
WHEN (`cdrs`.`snumber` = _LATIN1'') THEN 'Anonymous'
WHEN (`cdrs`.`snumber` IS NOT NULL) THEN `cdrs`.`snumber`
END) AS `called`,
(CASE
WHEN (`cdrs`.`stype` = _LATIN1'external') THEN 2
WHEN (`cdrs`.`dtype` = _LATIN1'external') THEN 3
END) AS `calltype`,
(CASE
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "01%" OR `cdrs`.`snumber` like "02%") THEN '01-02'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "080%") THEN '080'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "084%") THEN '084'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "087%") THEN '087'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "07%") THEN '07'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "09%") THEN '09'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "00%") THEN '00'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "04%") THEN '04'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "03%") THEN '03'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "02%") THEN '02'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "05%") THEN '05'
END) AS `innumber`,
(CASE
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "01%" OR `cdrs`.`dnumber` like "02%") THEN '01-02'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "080%") THEN '080'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "084%") THEN '084'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "087%") THEN '087'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "07%") THEN '07'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "09%") THEN '09'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "00%") THEN '00'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "04%") THEN '04'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "03%") THEN '03'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "02%") THEN '02'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "05%") THEN '05'
END) AS `outnumber`,
`cdrs`.`sname` AS `description`,
`cdrs`.`cnumber` AS `calling`,
`cdrs`.`cnumber` AS `DTN`,
FROM_UNIXTIME(`cdrs`.`start`) AS `start`,
FROM_UNIXTIME(`cdrs`.`end`) AS `end`,
FROM_UNIXTIME((`cdrs`.`end` - `cdrs`.`talktime`)) AS `answered`,
(`cdrs`.`end` - `cdrs`.`start`) AS `duration`,
`cdrs`.`talktime` AS `talktime`,
((`cdrs`.`end` - `cdrs`.`start`) - `cdrs`.`talktime`) AS `TTA`,
`cdrs`.`status` AS `status`
FROM `cdrs`
WHERE `cdrs`.`start` >= @_START and `cdrs`.`start` < @_END
) cdr1
WHERE (_CALLTYPE = 1 OR cdr1.calltype = _CALLTYPE)
GROUP BY cdr1.`callid`) cdr
JOIN customers ON id = cdr.dcustomer
WHERE (parent = _ID or cdr.dcustomer = _ID or parent IN
(SELECT id FROM customers WHERE parent = _ID))
AND HOUR(start) = h.idhour
) as 'count',
(
SELECT sum(talktime)
FROM (SELECT * FROM (
SELECT
`cdrs`.`stype` AS `stype`,
`cdrs`.`dtype` AS `dtype`,
`cdrs`.`uniqueid` AS `UniqueID`,
`cdrs`.`dcustomer` AS `dcustomer`,
`cdrs`.`callid` AS `callid`,
(CASE
WHEN (`cdrs`.`snumber` = _LATIN1'') THEN 'Anonymous'
WHEN (`cdrs`.`snumber` IS NOT NULL) THEN `cdrs`.`snumber`
END) AS `called`,
(CASE
WHEN (`cdrs`.`stype` = _LATIN1'external') THEN 2
WHEN (`cdrs`.`dtype` = _LATIN1'external') THEN 3
END) AS `calltype`,
(CASE
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "01%" OR `cdrs`.`snumber` like "02%") THEN '01-02'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "080%") THEN '080'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "084%") THEN '084'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "087%") THEN '087'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "07%") THEN '07'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "09%") THEN '09'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "00%") THEN '00'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "04%") THEN '04'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "03%") THEN '03'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "02%") THEN '02'
WHEN (`cdrs`.`stype` = _LATIN1'external' AND `cdrs`.`snumber` like "05%") THEN '05'
END) AS `innumber`,
(CASE
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "01%" OR `cdrs`.`dnumber` like "02%") THEN '01-02'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "080%") THEN '080'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "084%") THEN '084'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "087%") THEN '087'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "07%") THEN '07'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "09%") THEN '09'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "00%") THEN '00'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "04%") THEN '04'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "03%") THEN '03'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "02%") THEN '02'
WHEN (`cdrs`.`dtype` = _LATIN1'external' AND `cdrs`.`dnumber` like "05%") THEN '05'
END) AS `outnumber`,
`cdrs`.`sname` AS `description`,
`cdrs`.`cnumber` AS `calling`,
`cdrs`.`cnumber` AS `DTN`,
FROM_UNIXTIME(`cdrs`.`start`) AS `start`,
FROM_UNIXTIME(`cdrs`.`end`) AS `end`,
FROM_UNIXTIME((`cdrs`.`end` - `cdrs`.`talktime`)) AS `answered`,
(`cdrs`.`end` - `cdrs`.`start`) AS `duration`,
`cdrs`.`talktime` AS `talktime`,
((`cdrs`.`end` - `cdrs`.`start`) - `cdrs`.`talktime`) AS `TTA`,
`cdrs`.`status` AS `status`
FROM `cdrs`
WHERE `cdrs`.`start` >= @_START and `cdrs`.`start` < @_END
) cdr1
WHERE (_CALLTYPE = 1 OR cdr1.calltype = _CALLTYPE)
GROUP BY cdr1.`callid`) cdr
JOIN customers ON id = cdr.dcustomer
WHERE (parent = _ID or cdr.dcustomer = _ID or parent IN
(SELECT id FROM customers WHERE parent = _ID))
AND HOUR(start) = h.idhour
) as 'duration'
FROM hub.hours h ORDER BY idhour;
END
问:如何实现所需的输出?这个存储过程可以简化/优化吗?