1

完全に機能するクエリがありますが、読み込みに 1.5 秒かかります。私はMySQLを初めて使用するので、クエリが最適ではないことはわかっていますが、これを最適化する方法はありますか? これらのクエリが 5 ~ 6 個あり、すべてを読み込むのに 10 秒以上かかります。

Select * From
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `JAN` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-01-01') As `JAN`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `FEB` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-02-01') As `FEB`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `MAR` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-03-01') As `MAR`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `APR` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-04-01') As `APR`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `MAY` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-05-01') As `MAY`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `JUN` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-06-01') As `JUN`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `JUL` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-07-01') As `JUL`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `AUG` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-08-01') As `AUG`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `SEP` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-09-01') As `SEP`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `OCT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-10-01') As `OCT`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `NOV` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-11-01') As `NOV`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `DEC` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-12-01') As `DEC`,
    (SELECT ROUND(SUM(TotalAHT) / CallHandled) as Total_Metric
        FROM
        (SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-01-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-02-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-03-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-04-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-05-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-06-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-07-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-08-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-09-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-10-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-11-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-12-01') As TotalAHT, 
        (SELECT SUM(`Calls_Handled_Ct`) as `CallHandled` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id') As CallHandled) As `Total_Metric`

編集

以下の答えで、私が望む数字を得ることができました。すべてを 1 列にするのではなく、列ごとに結果を作成する方法はありますか?

期待される結果:

JAN     FEB     MAR     APR     MAY     JUN     JUL     AUG     
148     168     175     160     165     145     162     143
4

2 に答える 2

2

月ごとにグループ化された統計を探しているようで、グループ化を使用する代わりに、月ごとにクエリを実行します。私は次のようなものを提案します:

SELECT 
    ROUND(
           (
            SUM(`I_Talk_Time_Sec`) + 
            SUM(`Hold_Time_Sec`) + 
            SUM(`I_Work_Time_Sec`) + 
            SUM(I_AUX_Out_Time_Sec)
           ) / SUM(`Calls_Handled_Ct`)
         ) AS time_spent, 
     YEAR(Month_Date) stats_year, 
     MONTH(Month_Date) AS stats_month 
FROM `enterprise_rep_agent_stats`
WHERE `Employee_ID` = '$id'
GROUP BY stats_year, stats_month;
于 2013-09-09T06:58:15.057 に答える
0

If I managed to read it correctly (my eyes started to hurt at some point) something like this should work:

SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as sum, 
DATE_FORMAT(NOW(),'%Y-%m') as month
FROM enterprise_rep_agent_stats 
GROUP BY month
WITH ROLLUP
于 2013-09-09T06:56:54.913 に答える