[英]Split single row into multiple rows based on Date and time in SQL Server
I want to split a single row into multiple rows based on time. 我想根据时间将单行拆分为多行。 below are the example.
以下是示例。
SrNo Notification StartDate EndDate
---------------------------------------------------------------------------
1 001003741915 2018-08-20 07:27:00.000 2018-08-21 16:23:00.000
2 001003779670 2018-08-21 03:36:00.000 2018-08-21 04:36:00.000
3 001003779830 2018-08-21 04:36:00.000 2018-08-21 21:35:00.000
Expected output is below: 预期输出如下:
SrNo Notification StartDate EndDate
---------------------------------------------------------------------------
1 001003741915 2018-08-20 07:27:00.000 2018-08-21 05:59:00.000
1 001003741915 2018-08-21 06:00:00.000 2018-08-21 16:23:00.000
2 001003779670 2018-08-21 03:36:00.000 2018-08-21 04:36:00.000
3 001003779830 2018-08-21 04:36:00.000 2018-08-21 05:59:00.000
3 001003779830 2018-08-21 06:00:00.000 2018-08-21 21:35:00.000
Day start from 06:00 AM to next day 06:00 AM. 一天从06:00 AM开始到第二天06:00 AM。 When EndDate time is grated than 06:00 AM then split this date in two rows.
如果EndDate时间是06:00 AM以后的时间,则将此日期分为两行。 first row end date is 2018-08-21 05:59:00.000 and next row start 2018-08-21 06:00:00.000.
第一行结束日期是2018-08-21 05:59:00.000,下一行开始2018-08-21 06:00:00.000。
You can achieve this by using recursive CTE 您可以通过使用递归CTE来实现
WITH CTE AS (
SELECT ID, Notification, StartDate, EndDate
FROM TAB1
UNION ALL
SELECT ID, Notification, DATEADD(DD,1,StartDate), EndDate
FROM CTE
WHERE cast(StartDate as date) < cast(EndDate as date)
)
SELECT * FROM CTE order by id
if there is only one day or less difference between startdate and enddate 如果开始日期和结束日期之间只有一天或更短的时间差
If we call your table t1: 如果我们将您的表格称为t1:
SELECT [SrNo]
,[Notification]
,[StartDate]
,[EndDate]
FROM [t1]
where DATEADD(MINUTE, 59, DATEADD(HOUR, 5, CAST(CAST(enddate AS DATE) AS DATETIME))) > enddate
union
SELECT [SrNo]
,[Notification]
,[StartDate]
,DATEADD(MINUTE, 59, DATEADD(HOUR, 5, CAST(CAST(enddate AS DATE) AS DATETIME))) [EndDate]
FROM [t1]
where DATEADD(MINUTE, 59, DATEADD(HOUR, 5, CAST(CAST(enddate AS DATE) AS DATETIME))) between startdate and enddate
union
SELECT [SrNo]
,[Notification]
,DATEADD(MINUTE, 00, DATEADD(HOUR, 6, CAST(CAST(enddate AS DATE) AS DATETIME))) [StartDate]
, [EndDate]
FROM [t1]
where DATEADD(MINUTE, 59, DATEADD(HOUR, 5, CAST(CAST(enddate AS DATE) AS DATETIME))) between startdate and enddate
order by srno
,enddate
Below Query will help you. 以下查询将为您提供帮助。
CREATE TABLE #test
(
Notifications varchar(50)
,StartDate datetime
,EndDate Datetime
,Id int
)
INSERT into #test
select '001003741915','2018-08-20 07:27:00.000','2018-08-21 16:23:00.000',1
UNION select '001003779670','2018-08-21 03:36:00.000','2018-08-21 04:36:00.000',2
UNION select '001003779830','2018-08-21 04:36:00.000','2018-08-21 21:35:00.000',3
UNION select '001003779835','2018-08-21 04:36:00.000','2018-08-24 21:35:00.000',4
;with cte
As ( SELECT
ID,Notifications,StartDate,dateadd(d, datediff(d, 1, StartDate+1), '06:00') as StartOfDay, EndDate,dateadd(d, datediff(d, 1, EndDate+1), '06:00') as EndDayOfDate
FROM #test
)
, Result
AS (
select Id
,Notifications
,StartDate
,CASE WHEN StartOfDay BETWEEN StartDate AND EndDate THEN StartOfDay
WHEN ENDDate <StartOfDay THEN ENDDate
WHEN ENDDate <EndDayOfDate THEN ENDDate
ELSE EndDayOfDate END AS EndDate
from cte
union ALL
Select T.Id
,T.Notifications
,R.EndDate As StartDate
,CASE WHEN R.EndDate+1 < T.EndDate THEN R.EndDate+1 ELSE T.EndDate END AS EndDate
from cte T
INNER JOIN Result R
ON R.Notifications=T.Notifications
WHERE R.EndDate <T.EndDate
)
SELECT * FROM Result order by id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.