簡體   English   中英

高級SQL選擇查詢(唯一行)(分組依據)

[英]Advanced SQL Select Query (unique rows) (group by)

我的數據庫正在保存公司撥打的電話。
我需要進行選擇查詢,以選擇所有唯一的人,並且應該為每個人選擇:

  • 來電數量
  • 撥出電話數
  • 總通話
  • 平均通話時間

我嘗試了此操作,並選擇了入站和出站電話。
病態解釋如何查看呼叫何時入站或出站。

填充OriginationDevice時, OriginationDevice DestinationName進行入站調用。
填充DestinationDeviceOriginationName進行出站調用。

我需要每個唯一的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', '');

我的預期結果在查詢中。

這應該做你所要求的:

 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM