高级SQL选择查询(唯一行)(分组依据)

时间:2018-12-11 15:30:50

标签: mysql sql

我的数据库正在保存公司拨打的电话。
我需要进行一次选择查询,以选择所有唯一身份的人,并为每个人选择:

  • 呼入电话数
  • 呼出电话数量
  • 总通话次数
  • 平均通话时间

我尝试了此操作,但选择了入站和出站电话。
病态的解释如何查看呼叫何时入站或出站。

填满OriginationDevice后,将对其进行DestinationName的入站呼叫。
填满DestinationDevice后,将向OriginationName发出呼出电话。

我需要每个唯一的DestinationNameOriginationName并列出入站,出站和总通话次数,当然还有平均通话时间。
我走的很远,但似乎无法在一个查询中获得入站和出站。

查看我的SQL Fiddle,您可以在其中帮助我!
谁能帮助我获得每个人的平均通话时间,呼出电话,呼入电话和总通话次数?

我的查询尝试:

SELECT * FROM (
   SELECT 
   IFNULL (SUM( CASE WHEN OriginationDevice != '' AND ConnectTime != '' THEN 
   DATEDIFF(ConnectTime, EndTime) ELSE null END ) /  COUNT(case when 
   OriginationDevice <> '' then 1 else null end), 0) as calltime,
   COUNT(case when OriginationDevice != '' then 1 else null end) as inbound,
   COUNT(case when DestinationDevice != '' AND OriginationDevice = '' then 1 
   else null end) as outbound,
   COUNT(*) as total,
   DestinationName

   FROM calls WHERE (YEAR(EndTime) = 2018 AND MONTH(EndTime) = 12) and 
   (OriginationDevice != '' or DestinationDevice != '')
   AND ConnectTime != ''  GROUP BY DestinationName
) as t1
WHERE total > 0  ORDER BY total DESC, calltime

这是表sql:

CREATE TABLE IF NOT EXISTS `calls` (
  `OriginationName` varchar(200) NOT NULL,
  `DestinationName` varchar(200) NOT NULL,
  `ConnectTime` DATETIME NOT NULL,
  `EndTime` DATETIME NOT NULL,
  `OriginationDevice` varchar(200) NOT NULL,
  `DestinationDevice` varchar(200) NOT NULL
) DEFAULT CHARSET=utf8;
INSERT INTO `calls` (`OriginationName`, `DestinationName`, `ConnectTime`, `EndTime`, `OriginationDevice`, `DestinationDevice`) VALUES
  ('Person 1', 'Person 5', '2018-12-11 11:26:12', '2018-12-11 11:26:18', '243', '(call routing)'),
  ('Person 2', 'Person 3', '2018-12-11 10:16:12', '2018-12-11 10:16:54', '', '(call routing)'),
  ('Person 5', 'Person 1', '2018-12-11 10:21:12', '2018-12-11 10:22:22', '', ''),
   ('Person 2', 'Person 1', '2018-12-11 11:26:12', '2018-12-11 11:26:52', '233', ''),
    ('Person 1', 'Person 4', '2018-12-11 12:26:12', '2018-12-11 12:28:25', '456', ''),
     ('', 'Person 1', '2018-12-11 14:56:12', '2018-12-11 14:57:24', '', '(call routing)'),
  ('Person 3', '', '2018-12-11 15:26:12', '2018-12-11 15:26:37', '223', '');

我的预期结果在查询中。

1 个答案:

答案 0 :(得分:0)

这应该按照您的要求进行:

 SELECT 
    Person,
    SUM(case when typology = 'outbounds' then calls_number else 0 end) as outbounds,
    SUM(case when typology = 'inbounds' then calls_number else 0 end) as inbounds,
    SUM(calls_number) as calls_number,
    case when SUM(calls) = 0 then 0 else SUM(callTime) / SUM(calls) end as avgCallTime
FROM(
SELECT 
    OriginationName as Person,
    SUM(case 
        when DestinationDevice != '' and ConnectTime != '' then (EndTime - ConnectTime) 
        else 0 end
    ) as callTime,
    SUM(case when DestinationDevice != '' then 1 else 0 end) as calls_number,
    'outbounds' as typology
FROM calls 
WHERE (YEAR(EndTime) = 2018 AND MONTH(EndTime) = 12) and ConnectTime != ''
    and OriginationName != ''
GROUP BY OriginationName
    union
SELECT 
    DestinationName,
    SUM(case 
        when OriginationDevice != '' and ConnectTime != '' then (EndTime - ConnectTime) 
        else 0 end
    ),
    SUM(case when OriginationDevice  != ''  then 1 else 0 end),
    'inbounds' as typology
FROM calls 
WHERE (YEAR(EndTime) = 2018 AND MONTH(EndTime) = 12) and ConnectTime != '' 
    and DestinationName != ''
GROUP BY DestinationName) as T
GROUP BY Person;

让我知道是否有问题,此示例工作https://sqltest.net/#386096

相关问题