将每月工作时间划分为每周工作时间 - SQL Server

时间:2017-01-13 11:23:24

标签: sql-server datetime week-number weekday weekend

我有一个要求,我必须使用以下每位员工的月度数据生成报告。该报告在每个月的第1天运行,并提供数据直到上个月的第1天。 要求是将这些数据分成每周数据。因此,如果每个月的第一天在“星期一”开始,那么本周应该有5个工作日,“星期二”为4个工作日,“星期三”为3个工作日等等。并根据员工工作的天数计​​算工作时间相应的一周。如果每周的周数变化,则报告应相应地显示每周的数据。

EmpName Date        WorkTime
User1   2016-10-18  NULL
User1   2016-10-20  06:00:38
User1   2016-10-21  07:41:44
User1   2016-10-24  06:35:53
User1   2016-10-25  06:29:03
User1   2016-10-26  07:25:09
User1   2016-10-31  07:49:12
User1   2016-11-03  09:23:05
User1   2016-11-05  NULL
User1   2016-11-07  09:18:38
User1   2016-11-08  09:16:01
User1   2016-11-09  08:05:03
User1   2016-11-11  09:00:43
User1   2016-11-16  09:18:14

以下是上述查询的预期结果。

WeekNum WeekDur         EmpName Planned     Actual
Week1   18/10 - 22/10   User1   32:00:00    13:42:22
Week2   23/10 - 29/10   User1   40:00:00    20:30:05
Week3   30/10 - 31/10   User1   8:00:00     7:49:12

注意:计划的小时数是根据工作日的数量计算的。意味着周一至周五,因此每天8小时将为每周5天提供40小时。但是,需要计算所有7天的实际小时数,这样如果有人在周末工作,实际情况可以相应地反映任何额外时间而不是计划时间。

注意:NULL表示员工没有正确进行滑入。

很抱歉,但我自己没有尝试任何东西,因为我对这种要求不熟悉并且在处理日期和处理方面缺乏经验。时间查询。

我希望我已提供所有信息,并要求所有人在遇到任何问题或疑惑时与我联系。

1 个答案:

答案 0 :(得分:0)

我认为你实际上可以拥有超过24小时的time数据类型,所以我已经把你的时间分成了不同的领域,让你做你想做的事情。您可以通过在cte中添加大日期case语句来取消group by,但这意味着会出现重复的代码,因此需要重复更新代码并且我需要重复更新代码懒:

declare @t table (EmpName nvarchar(10), WorkDate date, WorkTime time);
insert into @t values
 ('User1','20161018',NULL),('User1','20161020','06:00:38'),('User1','20161021','07:41:44'),('User1','20161024','06:35:53'),('User1','20161025','06:29:03'),('User1','20161026','07:25:09'),('User1','20161031','07:49:12'),('User1','20161103','09:23:05'),('User1','20161105',NULL),('User1','20161107','09:18:38'),('User1','20161108','09:16:01'),('User1','20161109','08:05:03'),('User1','20161111','09:00:43'),('User1','20161116','09:18:14');

with cte as
(
select EmpName
            -- Pick the later of either the start of the current week or the current month.
        ,case when dateadd(wk, datediff(wk,0,WorkDate), 0) < dateadd(month,datediff(month,0,WorkDate),0)
                then dateadd(month,datediff(month,0,WorkDate),0)    -- This calculates the start of the month.
                else dateadd(wk, datediff(wk,0,WorkDate), 0)        -- This calculated the start of the week.
                end as WeekStart

            -- Pick the earlier of either the end of the current week or the current month.
        ,case when dateadd(d,6,dateadd(wk, datediff(wk,0,WorkDate), 0)) >= dateadd(month,datediff(month,0,WorkDate)+1,0)
                then dateadd(d,-1,dateadd(month,datediff(month,0,WorkDate)+1,0))    -- This calculates the last day of the month.
                else dateadd(d,6,dateadd(wk, datediff(wk,0,WorkDate), 0))           -- This calculates the last day of the week.
                end as WeekEnd

            -- Pick the earlier of either the friday of the current week or or the end of the current month.
        ,case when dateadd(d,4,dateadd(wk, datediff(wk,0,WorkDate), 0)) > case when dateadd(d,6,dateadd(wk, datediff(wk,0,WorkDate), 0)) >= dateadd(month,datediff(month,0,WorkDate)+1,0)
                                                                                then dateadd(d,-1,dateadd(month,datediff(month,0,WorkDate)+1,0))    -- This calculates the last day of the month.
                                                                                else dateadd(d,6,dateadd(wk, datediff(wk,0,WorkDate), 0))           -- This calculates the last day of the week.
                                                                                end

                then case when dateadd(d,6,dateadd(wk, datediff(wk,0,WorkDate), 0)) >= dateadd(month,datediff(month,0,WorkDate)+1,0)
                            then dateadd(d,-1,dateadd(month,datediff(month,0,WorkDate)+1,0))    -- This calculates the last day of the month.
                            else dateadd(d,6,dateadd(wk, datediff(wk,0,WorkDate), 0))           -- This calculates the last day of the week.
                            end
                else dateadd(d,4,dateadd(wk, datediff(wk,0,WorkDate), 0))           -- This calculates the Friday of the week.
                end as WorkingWeekEnd

        ,datepart(hour,WorkTime) as HoursWorked
        ,datepart(minute,WorkTime) as MinutesWorked
        ,datepart(second,WorkTime) as SecondsWorked
from @t
)
select EmpName
        ,WeekStart
        ,WeekEnd
        ,WorkingWeekEnd
        ,avg(datediff(d,WeekStart,WorkingWeekEnd)+1) * 8 as PlannedHoursWorked
        ,isnull(sum(HoursWorked),0) as HoursWorked
        ,isnull(sum(MinutesWorked),0) as MinutesWorked
        ,isnull(sum(SecondsWorked),0) as SecondsWorked
from cte
group by EmpName
        ,WeekStart
        ,WeekEnd
        ,WorkingWeekEnd
order by EmpName
        ,WeekStart;

输出:

╔═════════╦═════════════════════════╦═════════════════════════╦═════════════════════════╦════════════════════╦═════════════╦═══════════════╦═══════════════╗
║ EmpName ║        WeekStart        ║         WeekEnd         ║     WorkingWeekEnd      ║ PlannedHoursWorked ║ HoursWorked ║ MinutesWorked ║ SecondsWorked ║
╠═════════╬═════════════════════════╬═════════════════════════╬═════════════════════════╬════════════════════╬═════════════╬═══════════════╬═══════════════╣
║ User1   ║ 2016-10-17 00:00:00.000 ║ 2016-10-23 00:00:00.000 ║ 2016-10-21 00:00:00.000 ║                 40 ║          13 ║            41 ║            82 ║
║ User1   ║ 2016-10-24 00:00:00.000 ║ 2016-10-30 00:00:00.000 ║ 2016-10-28 00:00:00.000 ║                 40 ║          19 ║            89 ║            65 ║
║ User1   ║ 2016-10-31 00:00:00.000 ║ 2016-10-31 00:00:00.000 ║ 2016-10-31 00:00:00.000 ║                  8 ║           7 ║            49 ║            12 ║
║ User1   ║ 2016-11-01 00:00:00.000 ║ 2016-11-06 00:00:00.000 ║ 2016-11-04 00:00:00.000 ║                 32 ║           9 ║            23 ║             5 ║
║ User1   ║ 2016-11-07 00:00:00.000 ║ 2016-11-13 00:00:00.000 ║ 2016-11-11 00:00:00.000 ║                 40 ║          35 ║            39 ║            85 ║
║ User1   ║ 2016-11-14 00:00:00.000 ║ 2016-11-20 00:00:00.000 ║ 2016-11-18 00:00:00.000 ║                 40 ║           9 ║            18 ║            14 ║
╚═════════╩═════════════════════════╩═════════════════════════╩═════════════════════════╩════════════════════╩═════════════╩═══════════════╩═══════════════╝