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

时间:2016-03-01 10:14:32

标签: mysql dayofweek days

我现在的查询为我提供了所选日期的数据。我希望在接下来的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 | %day 1 | %day 2 | %第3天,依此类推7天

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

    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;

答案 2 :(得分:0)

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

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

FROM shift_report 在date&gt; = 20160223 AND team = 4 GROUP BY shift_report.advisor ORDER BY total_time DESC;

无论如何我可以使用第一天并根据第一次日期输入自动完成剩余的6天吗?

答案 3 :(得分:0)

使用以下方法管理以实现此目的:

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;

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