with recursive cte (id, dt, minutes) as
(
select
id,
date(created_at),
case
when dayofweek(created_at) in (1, 7) then 0
else timestampdiff(minute, created_at, least(now(), date(created_at) + interval 1 day))
end
from mytable
union all
select
id,
dt + interval 1 day,
case
when dayofweek(dt + interval 1 day) in (1, 7) then 0
else timestampdiff(minute, dt + interval 1 day, least(now(), dt + interval 2 day))
end
from cte
where dt + interval 1 day <= curdate()
)
select id, round(sum(minutes) / 60, 1) as hours
from cte
group by id
order by id;
1条答案
按热度按时间oyt4ldly1#
在一种编程语言中,你可以写一个循环,并通过迭代数日来计算小时数。在sql中,可以对递归查询执行相同的操作:
演示:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=a08f96bc29fb8a51302da10679496e22