SQL从具有不同条件性能问题的同一表中选择

时间:2017-09-27 20:16:49

标签: sql

SELECT DISTINCT 
    s.Site AS SiteName, 
    (SELECT IFNULL(SUM(sc.Count), 0) 
     FROM xmedius_api.faxcapday sc 
     WHERE sc.Date_Time BETWEEN '2017-08-28 04:00:00' AND '2017-09-27 18:42:05' 
       AND sc.Type = 'FaxSentGood' AND sc.Site = s.Site) AS SentCount, 
    (SELECT IFNULL(SUM(sfc.Count), 0) 
     FROM xmedius_api.faxcapday sfc 
     WHERE sfc.Date_Time BETWEEN '2017-08-28 04:00:00' AND '2017-09-27 18:42:05' 
       AND sfc.Type = 'FaxSentFail' AND sfc.Site = s.Site) AS SentFailedCount, 
    (SELECT IFNULL(SUM(rfc.Count), 0) 
     FROM xmedius_api.faxcapday rfc 
     WHERE rfc.Date_Time BETWEEN '2017-08-28 04:00:00' AND '2017-09-27 18:42:05' 
       AND rfc.Type = 'FaxReceivedFail' AND rfc.Site = s.Site) AS RecdFailedCount, 
    (SELECT IFNULL(SUM(rc.Count), 0) 
     FROM xmedius_api.faxcapday rc 
     WHERE rc.Date_Time BETWEEN '2017-08-28 04:00:00' AND '2017-09-27 18:42:05' 
       AND rc.Type = 'FaxReceivedGood' AND rc.Site = s.Site) AS RecdCount 
FROM 
    xmedius_api.faxcapday s 
WHERE 
    s.Date_Time BETWEEN '2017-08-28 04:00:00' AND '2017-09-27 18:42:05' 
    AND s.Site = 'system'
GROUP BY 
    s.Site 
ORDER BY 
    SentCount DESC 
LIMIT 15

以上查询效果很好但我正在尝试多用途进行其他搜索。我需要能够删除以下内容以获取所有站点的值(不仅仅是特定情况)

当我删除以下内容时,查询基本上就会自行解决:

AND s.Site = 'system'

我尝试了很多此查询的组合,但没有工作。让这个工作的最佳技巧是什么?

0 个答案:

没有答案