我有一个用户输入时间表,如下所示:
表名是时间表:
领域:
TimeBillingId int
UserId int
StartTime datetime
StopTime datetime
ElapsedTime time(7)
NormalTime time(7)
OverTime time(7)
现在我想要一份报告,我想在其中计算所有用户的总时间。像 Total ElapsedTime、按月计算的总正常时间和总加班时间。
但我的月份是 15 号到 15 号。所以我希望结果应该是这样的:
User Month TotalTime NormalTime OverTime
1 March-April 120:58:00 100:58:00 20:00:00
2 March-April 97:40:23 97:40:23 00:00:00
1 April-May 15:00:00 14:30:00 00:30:00
2 April-May 89:30:00 80:15:00 09:15:00
我正在使用此查询使其按月工作:
select MONTH(tym.StopTIme) as Month, YEAR(tym.StopTime) as Year, U.UserId,
ISNULL((select cast(sum(datediff(second,0,t.ElapsedTime))/3600 as varchar(20)) + ':' + right('0' + cast(sum(datediff(second,0,t.ElapsedTime))/60%60 as varchar(20)),2) + ':' + right('0' + cast(sum(datediff(second,0,t.ElapsedTime))%60 as varchar(20)),2)
from TimeBilling t where MONTH(t.StopTime) = MONTH(tym.StopTime) and YEAR(t.StopTime) = YEAR(tym.StopTime) and t.UserId = u.UserId),'00:00:00') as TotalTime,
ISNULL((select cast(sum(datediff(second,0,t.NormalTime))/3600 as varchar(20)) + ':' + right('0' + cast(sum(datediff(second,0,t.NormalTime))/60%60 as varchar(20)),2) + ':' + right('0' + cast(sum(datediff(second,0,t.NormalTime))%60 as varchar(20)),2)
from TimeBilling t where MONTH(t.StopTime) = MONTH(tym.StopTime) and YEAR(t.StopTime) = YEAR(tym.StopTime) and t.UserId = u.UserId),'00:00:00') as NormalTime,
ISNULL((select cast(sum(datediff(second,0,t.OverTime))/3600 as varchar(20)) + ':' + right('0' + cast(sum(datediff(second,0,t.OverTime))/60%60 as varchar(20)),2) + ':' + right('0' + cast(sum(datediff(second,0,t.OverTime))%60 as varchar(20)),2)
from TimeBilling t where MONTH(t.StopTime) = MONTH(tym.StopTime) and YEAR(t.StopTime) = YEAR(tym.StopTime) and t.UserId = u.UserId),'00:00:00') as OverTime
from TimeBilling tym, Users u
where tym.UserId = u.UserId
我得到了每个月的总时间,比如 1 日到 30 日/31 日。但我想从 15 号到 15 号。但我不知道如何在 sql 查询或存储过程中获取它。
谁能帮帮我?