我无法创建包含两个或多个不同项目的日期列表。这就是我的数据
ORA-12505, TNS: listener does not currently know of SID
这可能吗?谢谢
答案 0 :(得分:3)
您可以尝试使用CTE recursion
来制作。
关键是让startTime
列添加一天,然后在DateTime
条件下将Date
转换为CTE recursion
。
CREATE TABLE T(
ID INT,
[Start Date Time] DATETIME,
[End Date Time] DATETIME
);
INSERT INTO T VALUES (1,'2018-08-06 13:00:00.000','2018-08-07 10:00:00.000');
INSERT INTO T VALUES (2,'2018-08-10 08:00:00.000','2018-08-10 15:00:00.000');
查询1 :
;WITH CTE AS (
SELECT ID,[Start Date Time] startTime,[End Date Time] endTime
FROM T
UNION ALL
SELECT ID,CAST(CAST(DATEADD(DAY,1,startTime)AS DATE) AS DATETIME),endTime
FROM CTE
WHERE CAST(CAST(DATEADD(DAY,1,startTime) AS DATE) AS DATETIME) < endTime
)
SELECT id,
startTime AS 'Start Date Time',
(CASE WHEN LEAD(startTime,1,endTime) OVER(PARTITION BY ID ORDER BY startTime)= endTime
THEN endTime
ELSE DATEADD(second,-1,LEAD(startTime,1,endTime) OVER(PARTITION BY ID ORDER BY startTime))
END) AS 'End Date Time'
FROM CTE
Results :
| id | Start Date Time | End Date Time |
|----|----------------------|----------------------|
| 1 | 2018-08-06T13:00:00Z | 2018-08-06T23:59:59Z |
| 1 | 2018-08-07T00:00:00Z | 2018-08-07T10:00:00Z |
| 2 | 2018-08-10T08:00:00Z | 2018-08-10T15:00:00Z |