![](/img/trans.png)
[英]How to return each week data as array in mysql query grouped by week?
[英]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;
我想看看:
顾问| 团队| 周开始| total_time | 第1天的百分比| 第2天的百分比| %第3天,依此类推,持续7天
您可以在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 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;
感谢到目前为止的答复,我现在使用以下命令显示正确的数据:
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
> = 20160223, team
= 4 GROUP BY shift_report.advisor ORDER BY total_time DESC;
无论如何,我可以使用第一天并根据输入的第一天自动完成剩余的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;
注意:%% sdate %%和%% team %%替换为从php页面传入的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.