答案 0 :(得分:0)
尝试一下:
Declare @Tab Table(Deal int,Type varchar(10),person Varchar(10), StartDate DATETIME, EndDate DATETIME)
Insert into @Tab
Select 12345,'Gas','Bob','2019/1/1','2019/1/5' Union All
Select 44444,'Oil','Sue','2019/1/1','2019/1/3'
;WITH cte AS
(
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) - 1 AS [Incrementor]
FROM [master].[sys].[columns] sc1
CROSS JOIN [master].[sys].[columns] sc2
)
SELECT t.*,DATEADD(DAY, cte.[Incrementor], t.StartDate) As [Date]
FROM cte
CROSS JOIN @Tab t
WHERE DATEADD(DAY, cte.[Incrementor], t.StartDate) <= t.EndDate
Order by t.Deal
答案 1 :(得分:0)
请尝试以下操作:
;WITH rs AS (
SELECT t.Deal,t.StartDate AS [Date] FROM @Table t
UNION ALL
SELECT rs.Deal,DATEADD(DAY,1,rs.[Date]) AS [Date]
FROM rs
INNER JOIN @Table t ON t.Deal = rs.Deal
WHERE t.EndDate > rs.[Date]
)
SELECT t.Deal,t.StartDate,t.EndDate,rs.[Date]
FROM @Table t
INNER JOIN rs on rs.Deal = t.Deal
ORDER BY rs.Deal,rs.[Date]
;