MySql每月持续时间矩阵

时间:2012-09-02 14:30:54

标签: sql matrix duration

我正在尝试编写一个单独的查询,该查询将显示每月工时的矩阵。我正在努力实现这一目标。

enter image description here

我目前有这个。 enter image description here

正确的输出应该是 enter image description here

我的SQL是

`SELECT WEEK(start) as weekId,
                (
                    SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start)))
                    FROM tbl_time_clock
                    WHERE WEEKDAY(start)=0
                ) AS Monday,
                (
                    SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start)))
                    FROM tbl_time_clock
                    WHERE WEEKDAY(start)=1
                ) AS Tuesday,
                (
                    SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start)))
                    FROM tbl_time_clock
                    WHERE WEEKDAY(start)=2
                ) AS Wednesday,
                (
                    SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start)))
                    FROM tbl_time_clock
                    WHERE WEEKDAY(start)=3
                ) AS Thursday,
                (
                    SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start)))
                    FROM tbl_time_clock
                    WHERE WEEKDAY(start)=4
                ) AS Friday,
                (
                    SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start)))
                    FROM tbl_time_clock
                    WHERE WEEKDAY(start)=5
                ) AS Saturday,
                (
                    SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start)))
                    FROM tbl_time_clock
                    WHERE WEEKDAY(start)=6
                ) AS Sunday
            FROM tbl_time_clock
            WHERE MONTH(start) = {$monthId} AND user_id = {$userId}
            GROUP BY WEEK(start)`

表格如

╔═════════╦═════════════════════╦═════════════════════╗
║ user_id ║        start        ║        stop         ║
╠═════════╬═════════════════════╬═════════════════════╣
║       1 ║ 2012-08-28 08:14:49 ║ 2012-08-28 10:14:49 ║
║       1 ║ 2012-08-25 10:00:32 ║ 2012-08-25 16:21:57 ║
╚═════════╩═════════════════════╩═════════════════════╝

感谢!!!

2 个答案:

答案 0 :(得分:2)

您不需要子查询。

通常只有按两个字段分组,并让您的编程语言进行演示(创建表格):

SELECT 
    WEEK(start) as weekId,
    WEEKDAY(start) as weekDay,
    TIME_TO_SEC(TIMEDIFF(stop, start))
FROM tbl_time_clock
WHERE MONTH(start) = {$monthId} AND user_id = {$userId}
GROUP BY WEEK(start), WEEKDAY(start)

如果你的SQL本身必须真正给出一个矩阵(但为什么?),试试这个:

SELECT 
    WEEK(start) as weekId,
    SUM( if( WEEKDAY(start)=0,TIME_TO_SEC(TIMEDIFF(stop, start))),0) as Monday,
    SUM( if( WEEKDAY(start)=1,TIME_TO_SEC(TIMEDIFF(stop, start))),0) as Tuesday,
    SUM( if( WEEKDAY(start)=2,TIME_TO_SEC(TIMEDIFF(stop, start))),0) as Wednesday,
    SUM( if( WEEKDAY(start)=3,TIME_TO_SEC(TIMEDIFF(stop, start))),0) as Thursday,
    SUM( if( WEEKDAY(start)=4,TIME_TO_SEC(TIMEDIFF(stop, start))),0) as Friday,
    SUM( if( WEEKDAY(start)=5,TIME_TO_SEC(TIMEDIFF(stop, start))),0) as Saturday,
    SUM( if( WEEKDAY(start)=6,TIME_TO_SEC(TIMEDIFF(stop, start))),0) as Sunday,
FROM tbl_time_clock
WHERE MONTH(start) = {$monthId} AND user_id = {$userId}
GROUP BY WEEK(start)

答案 1 :(得分:0)

看起来您需要约束每个子查询中的星期。为此,请在内部表中添加别名,以便可以在内部查询中引用外部查询的值。星期一子查询看起来像这样:

(
     SELECT SUM(TIME_TO_SEC(TIMEDIFF(stop, start)))
     FROM tbl_time_clock AS tbl_inner
     WHERE WEEKDAY(tbl_inner.start)=0
     AND WEEK(tbl_inner.start)=tbl_outer.week_start
) AS Monday

应在约束子查询执行之前对外表进行分组,否则每周会有多个结果。所以你的外部FROM子句应如下所示:

FROM (
    select WEEK(start) AS week_start FROM tbl_time_clock GROUP BY WEEK(start)
    WHERE MONTH(start) = {$monthId} AND user_id = {$userId}
) as tbl_outer
编辑:修正。此外,我同意Konerak可能不需要矩阵,他的SQL解决方案更好。离开这个,因为它显示了如何修改给定的SQL以获得解决方案。