查询顶部条目及其值的列表

时间:2014-11-03 15:51:36

标签: mysql

我有一个非常基本的表格,其中包含交易清单(在这种情况下,它们是“呼叫详细报告”记录);

CREATE TABLE `cdr_records` (
  `dateTimeOrigination` int(11) DEFAULT NULL,
  `callingPartyNumber` varchar(50) DEFAULT NULL,
  `originalCalledPartyNumber` varchar(50) DEFAULT NULL,
  `finalCalledPartyNumber` varchar(50) DEFAULT NULL,
  `pkid` varchar(50) NOT NULL DEFAULT '',
  `duration` int(11) DEFAULT NULL,
  `destDeviceName` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`pkid`)
)

当我查询此表中的事务时,我得到这样的输出;

from_unixtime(dateTimeOrigination) | callingPartyNumber | originalCalledPartyNumber | finalCalledPartyNumber | sec_to_time(duration) |
+------------------------------------+--------------------+---------------------------+------------------------+-----------------------+
| 2014-09-26 08:22:11                | 12345            | exampleNumber             | exampleNumber          | 02:49:54              |
| 2014-09-26 15:06:35                | 67891            | exampleNumber             | exampleNumber          | 02:39:46              |
| 2014-09-26 17:46:33                | 67891            | exampleNumber             | exampleNumber          | 02:37:13              |
| 2014-08-21 17:41:30                | 12345            | exampleNumber             | exampleNumber          | 02:23:55              |
| 2014-08-21 14:43:01                | 12345            | exampleNumber             | exampleNumber          | 02:01:56

我想写一些做两件事的查询;

1)根据通话时间告诉我谁是最高级别的谈话者 2)该特定用户的所有呼叫的总持续时间是该期限

这样的事怎么接近?如果使用DISTINCT查询完成此操作,如何总结每个条目的持续时间值总数?

1 个答案:

答案 0 :(得分:1)

SELECT callingPartyNumber, sec_to_time(duration_sum)
FROM
(
   SELECT callingPartyNumber, sum(duration) as duration_sum 
   FROM cdr_records
   WHERE dateTimeOrigination
      BETWEEN UNIX_TIMESTAMP('014-09-26 08:22:11')
          AND UNIX_TIMESTAMP('2014-08-21 14:43:01')
   GROUP BY callingPartyNumber
   ORDER BY duration_sum
) sub1