今天在工作中遇到了一个很实际的问题,客户在OA接口的员工休假中间表中提供了连续时间段的休假记录,例如: 张三,2018-12-1 ~2018-12-31 ,病假,31天。这样带来的问题是,如果我需要统计张三从12月1号到12月15号的休假天数,单从这一整条连续记录是无法统计的。这时候就需要我们将一条长记录进行拆分。
这里记录下我自己的思路:
1:利用系统表得到0-2047的序列 ,2048*2048绝对够用了吧
SELECT sv.number AS n FROM MASTER.dbo.spt_values AS sv WHERE sv.[type]=‘P‘
结果如图:
2:利用CROSS APPLY 得到最终序列(从1开始,最后一行是2048*2048)
WITH t1 AS (SELECT sv.number AS n FROM MASTER.dbo.spt_values AS sv WHERE sv.[type]=‘P‘), t2 AS ( SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS rid FROM t1 AS a CROSS APPLY t1 AS b) ----------------- SELECT *FROM t2
结果如图
你千万别告诉我400多万都不够你拆的啊,小心我揍你
3:奉上完整代码
DECLARE @begindate DATE =CAST(‘2017-12-1‘ AS date) DECLARE @enddate DATE =CAST(‘2017-12-31‘ AS date) BEGIN WITH t1 AS (SELECT sv.number AS n FROM MASTER.dbo.spt_values AS sv WHERE sv.[type]=‘P‘), t2 AS ( SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS rid FROM t1 AS a CROSS APPLY t1 AS b) ----------------- --SELECT *FROM t2 SELECT DATEADD(DAY,t2.rid-1,@begindate) as date FROM t2 WHERE t2.rid BETWEEN -1 AND (DATEDIFF(DAY,@begindate,@enddate)+1) END
PS:因为序列从1开始,用Dateadd函数我需要从0天开始加起(作为第一天),因此注意下上述代码的-1 和+1的作用。
效果如图:
最后:
关于With 关键字、row_nunber()over()开窗函数 以及cross apply请您自行脑补学习,以免一知半解
原文地址:https://www.cnblogs.com/randy-fu/p/8715786.html
时间: 2024-10-09 18:39:00