您可以在 SUM 聚合函数中使用条件语句,因此每天计算:-
SELECT shift_report.advisor,
shift_report.team,
MIN(shift_report.`date`) AS week_commencing,
SUM(shift_report.time) as total_time,
ROUND(SUM(shift_report.time)/450 * 100,2) as percentage,
ROUND(100 * SUM(IF(shift_report.`date` = 20160223, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day1,
ROUND(100 * SUM(IF(shift_report.`date` = 20160224, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day2,
ROUND(100 * SUM(IF(shift_report.`date` = 20160225, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day3,
ROUND(100 * SUM(IF(shift_report.`date` = 20160226, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day4,
ROUND(100 * SUM(IF(shift_report.`date` = 20160227, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day5,
ROUND(100 * SUM(IF(shift_report.`date` = 20160228, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day6,
ROUND(100 * SUM(IF(shift_report.`date` = 20160229, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day7,
ROUND(100 * SUM(IF(shift_report.`date` = 20160301, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day8
FROM shift_report
WHERE `date` >=20160223
AND `date`<=20160301
AND `team`=4
GROUP BY shift_report.advisor,
shift_report.team
ORDER BY percentage DESC;
编辑
如果您只想插入 1 个日期,那么您可以这样做:-
SELECT shift_report.advisor,
shift_report.team,
MIN(shift_report.`date`) AS week_commencing,
SUM(shift_report.time) as total_time,
ROUND(SUM(shift_report.time)/450 * 100,2) as percentage,
ROUND(100 * SUM(IF(shift_report.`date` = start_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day1,
ROUND(100 * SUM(IF(shift_report.`date` = plus_1_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day2,
ROUND(100 * SUM(IF(shift_report.`date` = plus_2_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day3,
ROUND(100 * SUM(IF(shift_report.`date` = plus_3_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day4,
ROUND(100 * SUM(IF(shift_report.`date` = plus_4_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day5,
ROUND(100 * SUM(IF(shift_report.`date` = plus_5_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day6,
ROUND(100 * SUM(IF(shift_report.`date` = plus_6_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day7,
ROUND(100 * SUM(IF(shift_report.`date` = end_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day8
FROM shift_report
INNER JOIN
(
SELECT '2016-02-23' AS start_date,
DATE_ADD('2016-02-23',INTERVAL 1 DAY ) AS plus_1_date,
DATE_ADD('2016-02-23',INTERVAL 2 DAY ) AS plus_2_date,
DATE_ADD('2016-02-23',INTERVAL 3 DAY ) AS plus_3_date,
DATE_ADD('2016-02-23',INTERVAL 4 DAY ) AS plus_4_date,
DATE_ADD('2016-02-23',INTERVAL 5 DAY ) AS plus_5_date,
DATE_ADD('2016-02-23',INTERVAL 6 DAY ) AS plus_6_date,
DATE_ADD('2016-02-23',INTERVAL 7 DAY ) AS end_date
) sub01
WHERE `date` BETWEEN start_date AND end_date
AND `team`=4
GROUP BY shift_report.advisor,
shift_report.team
ORDER BY percentage DESC;
如果您只想在 1 个地方插入 1 个日期,则如下所示:-
SELECT shift_report.advisor,
shift_report.team,
MIN(shift_report.`date`) AS week_commencing,
SUM(shift_report.time) as total_time,
ROUND(SUM(shift_report.time)/450 * 100,2) as percentage,
ROUND(100 * SUM(IF(shift_report.`date` = start_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day1,
ROUND(100 * SUM(IF(shift_report.`date` = plus_1_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day2,
ROUND(100 * SUM(IF(shift_report.`date` = plus_2_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day3,
ROUND(100 * SUM(IF(shift_report.`date` = plus_3_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day4,
ROUND(100 * SUM(IF(shift_report.`date` = plus_4_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day5,
ROUND(100 * SUM(IF(shift_report.`date` = plus_5_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day6,
ROUND(100 * SUM(IF(shift_report.`date` = plus_6_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day7,
ROUND(100 * SUM(IF(shift_report.`date` = end_date, shift_report.time, 0)) / SUM(shift_report.time), 2) AS Day8
FROM shift_report
INNER JOIN
(
SELECT base_start_date AS start_date,
DATE_ADD(base_start_date,INTERVAL 1 DAY ) AS plus_1_date,
DATE_ADD(base_start_date,INTERVAL 2 DAY ) AS plus_2_date,
DATE_ADD(base_start_date,INTERVAL 3 DAY ) AS plus_3_date,
DATE_ADD(base_start_date,INTERVAL 4 DAY ) AS plus_4_date,
DATE_ADD(base_start_date,INTERVAL 5 DAY ) AS plus_5_date,
DATE_ADD(base_start_date,INTERVAL 6 DAY ) AS plus_6_date,
DATE_ADD(base_start_date,INTERVAL 7 DAY ) AS end_date
FROM
(
SELECT '2016-02-23' AS base_start_date
) sub2
) sub1
WHERE `date` BETWEEN start_date AND end_date
AND `team`=4
GROUP BY shift_report.advisor,
shift_report.team
ORDER BY percentage DESC;
请注意,这两种方法都避免在 WHERE 子句中进行日期计算(否则可能会触发对每行数据的计算,而不是只为查询一次)