将选择语句与日期结合

时间:2019-05-17 10:28:32

标签: php sql database

请帮助我,我希望将SQL Select语句合并到一个查询中

<?php

Total Assigned = SELECT date(DATE_DISTRIBUTE)
     , COUNT(DATE_DISTRIBUTE) AS TotalAssigned
  FROM ata_report_extracted WHERE STATUS ='DISTRIBUTED'
 GROUP BY date(DATE_DISTRIBUTE) ";


Total Handled = "SELECT date(DATE_HANDLED)
     , COUNT(DATE_HANDLED) AS TotalHandled
  FROM ata_report_extracted WHERE PROS_DESCRIPTION NOT IN ('Open', 'Acknowledged', 'Fallout', 'Cleared') AND STATUS ='DISTRIBUTED'
 GROUP BY date(DATE_HANDLED) ";

Total Resolved ="SELECT date(DATE_HANDLED)
     , COUNT(DATE_HANDLED) AS TotalResolved
  FROM ata_report_extracted WHERE PROS_DESCRIPTION = 'Closed' AND STATUS ='DISTRIBUTED'
 GROUP BY date(DATE_HANDLED) ";

TotalDispatch ="SELECT date(DATE_HANDLED)
     , COUNT(DATE_HANDLED) AS TotalDispatch
  FROM ata_report_extracted WHERE PROS_DESCRIPTION ='Dispatch' AND STATUS ='DISTRIBUTED'
 GROUP BY date(DATE_HANDLED) ";


TotalPending ="SELECT date(DATE_HANDLED)
     , COUNT(DATE_HANDLED) AS TotalPending
  FROM ata_report_extracted WHERE PROS_DESCRIPTION IN ('TOKUNDEROB', 'CALLNOANSWER') AND STATUS ='DISTRIBUTED'
 GROUP BY date(DATE_HANDLED) ";

 ?>


<?php

Total Assigned = SELECT date(DATE_DISTRIBUTE)
     , COUNT(DATE_DISTRIBUTE) AS TotalAssigned
  FROM ata_report_extracted WHERE STATUS ='DISTRIBUTED'
 GROUP BY date(DATE_DISTRIBUTE) ";


Total Handled = "SELECT date(DATE_HANDLED)
     , COUNT(DATE_HANDLED) AS TotalHandled
  FROM ata_report_extracted WHERE PROS_DESCRIPTION NOT IN ('Open', 'Acknowledged', 'Fallout', 'Cleared') AND STATUS ='DISTRIBUTED'
 GROUP BY date(DATE_HANDLED) ";

Total Resolved ="SELECT date(DATE_HANDLED)
     , COUNT(DATE_HANDLED) AS TotalResolved
  FROM ata_report_extracted WHERE PROS_DESCRIPTION = 'Closed' AND STATUS ='DISTRIBUTED'
 GROUP BY date(DATE_HANDLED) ";

TotalDispatch ="SELECT date(DATE_HANDLED)
     , COUNT(DATE_HANDLED) AS TotalDispatch
  FROM ata_report_extracted WHERE PROS_DESCRIPTION ='Dispatch' AND STATUS ='DISTRIBUTED'
 GROUP BY date(DATE_HANDLED) ";


TotalPending ="SELECT date(DATE_HANDLED)
     , COUNT(DATE_HANDLED) AS TotalPending
  FROM ata_report_extracted WHERE PROS_DESCRIPTION IN ('TOKUNDEROB', 'CALLNOANSWER') AND STATUS ='DISTRIBUTED'
 GROUP BY date(DATE_HANDLED) ";

 ?>

每天通过一个查询显示总报告

1 个答案:

答案 0 :(得分:0)

注释中解释的所有要求都包含在以下查询中。我使用了FULL JOIN,这样一来,如果一个日期中有一个日期可用,而另一个日期中没有一个日期,那么该日期也会出现在输出中。

SELECT 
CASE
    WHEN A.Date_Han_Dst IS NULL THEN B.Date_Han_Dst 
    ELSE A.Date_Han_Dst
END AS [Date_Han_Dst],
A.TotalHandled,
A.TotalResolved,
A.TotalDispatch,
A.TotalPending,
B.TotalAssigned
FROM 
(
    SELECT date(DATE_HANDLED)  AS [Date_Han_Dst],
    SUM(CASE WHEN PROS_DESCRIPTION NOT IN ('Open', 'Acknowledged', 'Fallout', 'Cleared') AND STATUS ='DISTRIBUTED' THEN 1 ELSE 0  END) AS TotalHandled,
    SUM(CASE WHEN PROS_DESCRIPTION = 'Closed' AND STATUS ='DISTRIBUTED' THEN 1 ELSE 0 END) AS TotalResolved,
    SUM(CASE WHEN PROS_DESCRIPTION ='Dispatch' AND STATUS ='DISTRIBUTED' THEN 1 ELSE 0 END) AS TotalDispatch,
    SUM(CASE WHEN PROS_DESCRIPTION IN ('TOKUNDEROB', 'CALLNOANSWER') AND STATUS ='DISTRIBUTED' THEN 1 ELSE 0  END) AS TotalPending
    FROM ata_report_extracted GROUP BY date(DATE_HANDLED)
)A
FULL JOIN
(
    SELECT date(DATE_DISTRIBUTE)  AS [Date_Han_Dst],
    SUM(CASE WHEN STATUS ='DISTRIBUTED' THEN 1 ELSE 0 END) TotalAssigned
    FROM ata_report_extracted GROUP BY date(DATE_DISTRIBUTE) 
)B
ON A.Date_Han_Dst = B.Date_Han_Dst