mysql 矩阵运算_MySql每月持续时间矩阵

博客内容涉及使用SQL查询从`tbl_time_clock`表中按月份和用户ID获取每周工作时间的总和。查询分别计算了周一到周日的工作秒数,并呈现为每月持续时间矩阵。示例数据包括用户的工作开始和结束时间。
摘要由CSDN通过智能技术生成

我正在尝试编写一个查询,它会显示工作小时的每月持续时间矩阵。我正试图做到这一点。

v8HG8.jpg

我现在有这个。

sUwoC.jpg

正确的输出应该是

IyUTE.jpg

我的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 ║

╚═════════╩═════════════════════╩═════════════════════╝谢谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值