在Sql Server 2012(T-SQL)上,我想分析缺席日期列表并重新排列数据以汇总信息,按月编目。
示例:
以下是原始数据列表
TimeSheetID UserID LeaveID StartDate EndDate Duration
1 779 1 01/01/2016 01/01/2016 7.5
2 779 1 02/01/2016 02/01/2016 7.5
3 779 2 03/01/2016 03/01/2016 7.5
4 1021 3 01/01/2016 02/01/2016 7.5
5 999 4 30/01/2016 30/01/2016 7.5
6 999 4 31/01/2016 31/01/2016 7.5
7 999 4 01/02/2016 01/02/2016 7.5
8 999 4 02/02/2016 02/02/2016 7.5
9 123 5 27/02/2016 28/02/2016 12
10 123 5 29/02/2016 01/03/2016 12
11 123 5 01/03/2016 02/03/2016 12
以下是首选输出,您可以看到在大多数情况下数据已汇总,但是如果日期过了新月份,则数据会被分隔到新的月份组中。
同样,如果日期范围超过两天,则将其归类为夜班,因此将开始日期作为分组因子并将其作为相应的月份分组。
UserID LeaveID StartDate EndDate Duration
779 1 01/01/2016 02/01/2016 15
779 2 03/01/2016 03/01/2016 7.5
1021 3 01/01/2016 02/01/2016 7.5
999 4 30/01/2016 31/01/2016 15
999 4 01/01/2016 02/02/2016 15
123 5 27/02/2016 01/03/2016 24
123 5 01/03/2016 02/03/2016 12
答案 0 :(得分:0)
我的最后两个值断开连接。 (在下面突出显示)
Select UserID
,LeaveID
,StartDate = Min(KeyDate)
,EndDate = Max(KeyDate)
,Duration = cast(Sum(PerDay) as decimal(10,2))
From (Select KeyDate=RetVal From [dbo].[udf-Range-Date]((Select min(StartDate) from @YourTable),(Select max(EndDate) from @YourTable),'DD',1) ) A
Join (Select *,PerDay = Duration/(1+DateDiff(DD,StartDate,EndDate)) From @YourTable) B on (A.KeyDate Between B.StartDate and B.EndDate)
Group By
UserID
,LeaveID
,Month(KeyDate)
Order By LeaveID
返回
现在,我使用TVF来创建动态日期范围,但是Tally / Calendar Table也可以解决这个问题。
CREATE FUNCTION [dbo].[udf-Range-Date] (@R1 datetime,@R2 datetime,@Part varchar(10),@Incr int)
Returns Table
Return (
with cte0(M) As (Select 1+Case @Part When 'YY' then DateDiff(YY,@R1,@R2)/@Incr When 'QQ' then DateDiff(QQ,@R1,@R2)/@Incr When 'MM' then DateDiff(MM,@R1,@R2)/@Incr When 'WK' then DateDiff(WK,@R1,@R2)/@Incr When 'DD' then DateDiff(DD,@R1,@R2)/@Incr When 'HH' then DateDiff(HH,@R1,@R2)/@Incr When 'MI' then DateDiff(MI,@R1,@R2)/@Incr When 'SS' then DateDiff(SS,@R1,@R2)/@Incr End),
cte1(N) As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
cte2(N) As (Select Top (Select M from cte0) Row_Number() over (Order By (Select NULL)) From cte1 a, cte1 b, cte1 c, cte1 d, cte1 e, cte1 f, cte1 g, cte1 h ),
cte3(N,D) As (Select 0,@R1 Union All Select N,Case @Part When 'YY' then DateAdd(YY, N*@Incr, @R1) When 'QQ' then DateAdd(QQ, N*@Incr, @R1) When 'MM' then DateAdd(MM, N*@Incr, @R1) When 'WK' then DateAdd(WK, N*@Incr, @R1) When 'DD' then DateAdd(DD, N*@Incr, @R1) When 'HH' then DateAdd(HH, N*@Incr, @R1) When 'MI' then DateAdd(MI, N*@Incr, @R1) When 'SS' then DateAdd(SS, N*@Incr, @R1) End From cte2 )
Select RetSeq = N+1
,RetVal = D
From cte3,cte0
Where D<=@R2
)
/*
Max 100 million observations -- Date Parts YY QQ MM WK DD HH MI SS
Syntax:
Select * from [dbo].[udf-Range-Date]('2016-10-01','2020-10-01','YY',1)
Select * from [dbo].[udf-Range-Date]('2016-01-01','2017-01-01','MM',1)
*/
答案 1 :(得分:0)