您需要添加循環以獲取開始和結束日期之間的日期,例如:
DECLARE @table table (ID Varchar(50), StartDate datetime,EndDate datetime)
insert into @table values ('AAA','2017-03-17 00:00:00.000',' 2017-03-19 00:00:00.000')
insert into @table values ('BB','2017-06-20 00:00:00.000 ',' 2017-06-25 00:00:00.000')
insert into @table values ('CC','2017-05-13 00:00:00.000 ',' 2017-05-17 00:00:00.000')
insert into @table values ('DD','2017-06-20 00:00:00.000 ',' 2017-05-27 00:00:00.000')
insert into @table values ('EE','2017-03-01 00:00:00.000 ',' 2017-03-05 00:00:00.000')
insert into @table values ('FF','2017-08-07 00:00:00.000 ',' 2017-08-11 00:00:00.000')
SELECT * FROM @table
SELECT id, StartDate FROM @table WHERE id='AAA'
union all
SELECT id, Dateadd(day,1,startdate) AS date FROM @table WHERE id='AAA' AND Dateadd(day,1,startdate)<EndDate
union all
SELECT id, EndDate FROM @table WHERE id='AAA'
union all
SELECT id, StartDate FROM @table WHERE id='BB'
union all
SELECT id, Dateadd(day,1,startdate) AS date FROM @table WHERE id='BB' AND Dateadd(day,1,startdate)<EndDate
union all
SELECT id, EndDate FROM @table WHERE id='BB'
union all
SELECT id, StartDate FROM @table WHERE id='CC'
union all
SELECT id, Dateadd(day,1,startdate) AS date FROM @table WHERE id='CC' AND Dateadd(day,1,startdate)<EndDate
union all
SELECT id, EndDate FROM @table WHERE id='CC'
union all
SELECT id, StartDate FROM @table WHERE id='DD'
union all
SELECT id, Dateadd(day,1,startdate) AS date FROM @table WHERE id='DD' AND Dateadd(day,1,startdate)<EndDate
union all
SELECT id, EndDate FROM @table WHERE id='DD'
union all
SELECT id, StartDate FROM @table WHERE id='EE'
union all
SELECT id, Dateadd(day,1,startdate) AS date FROM @table WHERE id='EE' AND Dateadd(day,1,startdate)<EndDate
union all
SELECT id, EndDate FROM @table WHERE id='EE'
union all
SELECT id, StartDate FROM @table WHERE id='FF'
union all
SELECT id, Dateadd(day,1,startdate) AS date FROM @table WHERE id='FF' AND Dateadd(day,1,startdate)<EndDate
union all
SELECT id, EndDate FROM @table WHERE id='FF'
Output:
id StartDate
AAA 2017-03-17 00:00:00.000
AAA 2017-03-18 00:00:00.000
AAA 2017-03-19 00:00:00.000
BB 2017-06-20 00:00:00.000
BB 2017-06-21 00:00:00.000
BB 2017-06-25 00:00:00.000
CC 2017-05-13 00:00:00.000
CC 2017-05-14 00:00:00.000
CC 2017-05-17 00:00:00.000
DD 2017-06-20 00:00:00.000
DD 2017-05-27 00:00:00.000
EE 2017-03-01 00:00:00.000
EE 2017-03-02 00:00:00.000
EE 2017-03-05 00:00:00.000
FF 2017-08-07 00:00:00.000
FF 2017-08-08 00:00:00.000
FF 2017-08-11 00:00:00.000
如果他們都開始在午夜,何苦存儲時間成分呢?也就是說,這是應用程序代碼 – Strawberry
的工作1. Mysql和ms sql server是具有不同sql實現的兩種不同產品。你使用哪一個?如果這些問題不包含任何誠實的解決問題的嘗試,通常在這裏就不會得到很好的答案。 – Shadow
時間在這裏沒關係。這就像留下數據庫的員工從開始日期到結束日期離開數據庫。所以我需要在@Strawberry –