Return 一周中每一天的数据(每行 7 条记录)- MySQL

Return Data for each day of week (7 records for each row)- MySQL

我目前的查询为我提供了选定日期的数据。我想从所选日期开始的接下来 7 天的一周中的每一天都有一个百分比:

select distinct(shift_report.advisor), team, shift_report.date as week_commencing, SUM(shift_report.time) as total_time, round(SUM(shift_report.time)/450 * 100,2) as percentage 
from shift_report
where `date` >=20160223
AND `date`<=20160301
AND `team`=4
GROUP BY shift_report.advisor ORDER BY percentage DESC;

我想看:

顾问 |团队 | week_commencing | total_time | % 第 1 天 | % 第 2 天 | % 第 3 天,依此类推 7 天

这行得通吗? 它有点硬编码,您可以尝试使其更复杂,但这是您可以利用的基本原则:

    SELECT advisor, team, week_commencing, total_time, 
           max(case when `date` = 20160223 then percentage end) day_1,
           max(case when `date` = 20160224 then percentage end) day_2,
           max(case when `date` = 20160225 then percentage end) day_3,
           max(case when `date` = 20160226 then percentage end) day_4,
           max(case when `date` = 20160227 then percentage end) day_5,
           max(case when `date` = 20160228 then percentage end) day_6,
           max(case when `date` = 20160229 then percentage end) day_7
    FROM (
       select distinct(shift_report.advisor) AS advisor, team,
             shift_report.date as week_commencing, SUM(shift_report.time) as    total_time, 
             round(SUM(shift_report.time)/450 * 100,2) as percentage 
       from shift_report
       where `date` >=20160223
         AND `date`<=20160301
         AND `team`=4
       GROUP BY shift_report.advisor ORDER BY percentage DESC
    ) your_query
   GROUP BY advisor, team, week_commencing, total_time;

您可以在 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 子句中进行日期计算(否则可能会触发对每一行数据的计算,而不是仅针对查询一次)

感谢到目前为止的回复,我现在可以使用以下方法显示正确的数据:

SELECT shift_report.advisor, 
shift_report.team, 
MIN(shift_report.`date`) AS week_commencing, 
SUM(shift_report.time) as total_time, 
ROUND(100 * SUM(IF(shift_report.`date` = 20160223, shift_report.time, 0)) / 450,2) AS Day1,
ROUND(100 * SUM(IF(shift_report.`date` = 20160224, shift_report.time, 0)) / 450,2) AS Day2,
ROUND(100 * SUM(IF(shift_report.`date` = 20160225, shift_report.time, 0)) / 450,2) AS Day3,
ROUND(100 * SUM(IF(shift_report.`date` = 20160226, shift_report.time, 0)) / 450,2) AS Day4,
ROUND(100 * SUM(IF(shift_report.`date` = 20160227, shift_report.time, 0)) / 450,2) AS Day5,
ROUND(100 * SUM(IF(shift_report.`date` = 20160228, shift_report.time, 0)) / 450,2) AS Day6,
ROUND(100 * SUM(IF(shift_report.`date` = 20160229, shift_report.time, 0)) / 450,2) AS Day7

来自shift_report 其中 date >=20160223 和 team=4 分组依据 shift_report.advisor 按 total_time 降序排序;

我是否可以使用第一天并根据第一个日期输入自动执行剩余 6 天?

设法使用以下方法使此工作正常进行:

SELECT advisor, 
MIN(shift_report.date) AS week_commencing, 
ROUND(100 * SUM(IF(shift_report.date = %%sdate%%, shift_report.time, 0)) / 450,2) AS Day1, 
ROUND(100 * SUM(IF(shift_report.date = %%sdate%% + INTERVAL 1 DAY, shift_report.time, 0)) / 450,2) AS Day2, 
ROUND(100 * SUM(IF(shift_report.date = %%sdate%% + INTERVAL 2 DAY, shift_report.time, 0)) / 450,2) AS Day3, 
ROUND(100 * SUM(IF(shift_report.date = %%sdate%% + INTERVAL 3 DAY, shift_report.time, 0)) / 450,2) AS Day4, 
ROUND(100 * SUM(IF(shift_report.date = %%sdate%% + INTERVAL 4 DAY, shift_report.time, 0)) / 450,2) AS Day5, 
ROUND(100 * SUM(IF(shift_report.date = %%sdate%% + INTERVAL 5 DAY, shift_report.time, 0)) / 450,2) AS Day6, 
ROUND(100 * SUM(IF(shift_report.date = %%sdate%% + INTERVAL 6 DAY, shift_report.time, 0)) / 450,2) AS Day7 
FROM shift_report WHERE date >=%%sdate%% 
%%team%% 
GROUP BY shift_report.advisor;

注意:%%date%% 和 %%team%% 替换为从 php 页面传入的数据。