您需要添加循环以获取开始和结束日期之间的日期,例如:
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 –