我有以下SQL,我希望在以下日期有差距。
declare @startdate datetime = '2017-05-01'
declare @enddate datetime = '2017-05-25'
create table #tmpdates (id int, date1 datetime, date2 datetime, rate int)
insert into #tmpdates values (1, '2017-05-05', '2017-05-15', 10)
insert into #tmpdates values (2, '2017-05-16', '2017-05-18', 12)
insert into #tmpdates values (3, '2017-05-21', '2017-05-25', 15)
select * from #tmpdates where date1 >= @startdate and date2 <= @enddate
drop table #tmpdates
因此输出应包含2017-05-01至2017-05-04和2017-05-19至2017-05-20 - 另外2条记录。
Output:
1 5/1/2017 0:00 5/4/2017 0:00 NO DATA
2 5/5/2017 0:00 5/15/2017 0:00 10
3 5/16/2017 0:00 5/18/2017 0:00 12
4 5/19/2017 0:00 5/20/2017 0:00 NO DATA
5 5/21/2017 0:00 5/25/2017 0:00 15
在我的上述查询中,只有日期范围记录返回..请指导或如何包含这些?
答案 0 :(得分:4)
这可以假设没有重叠的间隔。
declare @startdate datetime = '2017-05-16'
declare @enddate datetime = '2017-05-26'
create table #tmpdates (id int, date1 datetime, date2 datetime, rate int)
insert into #tmpdates values (0, '2017-04-01', '2017-04-25',22)
insert into #tmpdates values (1, '2017-05-05', '2017-05-15', 10)
insert into #tmpdates values (2, '2017-05-16', '2017-05-18', 12)
insert into #tmpdates values (3, '2017-05-21', '2017-05-25', 15)
declare @final_result table (date1 date, date2 date, rate int)
insert into @final_result
select @startdate,dateadd(day,-1,t.date1),null
from #tmpdates t
where @startdate < t.date1 and
t.date1 <= (select min(t1.date1) from #tmpdates t1 where t1.date1 >= @startdate)
union all
select date1, date2, rate
from #tmpdates
where (date1 >= @startdate or date2 >= @startdate) and
(date2 <= @enddate or date1 <= @enddate)
union all
select dateadd(day,1,t.date2),
( select dateadd(day,-1,min(t3.date1))
from #tmpdates t3 where t3.date1 > t.date2) ,
null
from #tmpdates t
where dateadd(day,1,t.date2) < (select min(t1.date1) from #tmpdates t1 where t1.date1 > t.date2)
and t.date1 >= @startdate and t.date2 <= @enddate
union all
select dateadd(day,1,max(t.date2)), @enddate, null
from #tmpdates t
having max(t.date2) < @enddate
drop table #tmpdates
select * from @final_result order by date1
修改强>
它从四个查询中收集数据并执行union all
。
第一个查询:
select @startdate,dateadd(day,-1,t.date1),null
from #tmpdates t
where @startdate < t.date1 and
t.date1 <= (select min(t1.date1) from #tmpdates t1 where t1.date1 >= @startdate)
如果在忽略@startdate
之前有间隔,则选择表中@startdate
和第一个(最小)日期之间的差距。因此,它会选择从@startdate
到大于@startdate
的时间间隔的第一个日期的差距(如果有的话)。
第二个查询:
select date1, date2, rate
from #tmpdates
where (date1 >= @startdate or date2 >= @startdate) and
(date2 <= @enddate or date1 <= @enddate)
从表中选择记录(非间隙)。如果@startdate
介于范围之间,则包含该记录。 @enddate
参数也是如此。
第三个查询:
select dateadd(day,1,t.date2),
( select dateadd(day,-1,min(t3.date1))
from #tmpdates t3 where t3.date1 > t.date2) ,
null
from #tmpdates t
where dateadd(day,1,t.date2) < (select min(t1.date1) from #tmpdates t1 where t1.date1 > t.date2)
and t.date1 >= @startdate and t.date2 <= @enddate
选择表格中最小和最大(介于@startdate
和@enddate
之间)间隔之间的差距。
最后 第四个查询:
select dateadd(day,1,max(t.date2)), @enddate, null
from #tmpdates t
having max(t.date2) < @enddate
选择表格中最大日期(@startdate
和@enddate
之间的最大日期)与@enddate
之间的差距,如果有差距的话。
所有这些记录都插入到@final_result
表中,以便按间隔排序。
答案 1 :(得分:2)
请使用以下查询:
DECLARE @STARTDATE DATE = '2017-05-01'
DECLARE @ENDDATE DATE = '2017-05-25'
DECLARE @DATES TABLE (ID INT, DATE1 DATE, DATE2 DATE, RATE INT)
INSERT INTO @DATES VALUES
(1, '2017-05-05', '2017-05-15', 10),
(2, '2017-05-16', '2017-05-19', 12),
(3, '2017-05-21', '2017-05-25', 15)
SELECT* FROM
(
SELECT @STARTDATE AS DATE1,DATEADD(DAY,-1,MIN(DATE1)) AS DATE2,'NO DATA'AS RATE FROM @DATES
UNION
SELECT
CASE WHEN
LEAD(DATE1) OVER (ORDER BY DATE1) = DATEADD(DAY,1,DATE2) THEN NULL
ELSE DATEADD(DAY,1,DATE2) END AS DATE1,
CASE WHEN
LEAD(DATE1) OVER (ORDER BY DATE1) = DATEADD(DAY,1,DATE2) THEN NULL
ELSE LEAD(DATEADD(DAY,-1,DATE1)) OVER (ORDER BY DATE1) END AS DATE2,
'NO DATA'AS RATE
FROM @DATES d
UNION
SELECT DATE1, DATE2,CAST(RATE AS NVARCHAR(10)) FROM @DATES
UNION
SELECT DATEADD(DAY,1,MAX(DATE2)) AS DATE1,@ENDDATE AS DATE2,'NO DATA'AS RATE FROM @DATES
) A WHERE A.DATE2 IS NOT NULL AND A.DATE1 <= A.DATE2
AND DATE1 >= @STARTDATE AND DATE2 <=@ENDDATE
ORDER BY A.DATE1
答案 2 :(得分:0)
您可以尝试以下代码。我正在从@StartDate走到@endDate并找到差距。
declare @startdate datetime = '2017-05-01'
declare @enddate datetime = '2017-05-04'
declare @startdate1 datetime, @enddate1 datetime
declare @dates table (date1 date,date2 date)
create table #tmpdates (id int, date1 datetime, date2 datetime, rate int)
insert into #tmpdates values (1, '2017-05-05', '2017-05-15', 10)
insert into #tmpdates values (2, '2017-05-16', '2017-05-18', 12)
insert into #tmpdates values (3, '2017-05-21', '2017-05-25', 15)
select * from #tmpdates where date1 >= @startdate and date2 <= @enddate
set @startdate1=@startdate
while @startdate1<=@enddate
begin
if not exists(select 1 from #tmpdates where @startdate1 between date1 and date2)
begin
if not exists (select 1 from @dates where @startdate1 > date1 and date2 is null)
begin
insert into @dates(date1)values(@startdate1)
end
else
begin
if @startdate1+1>=@enddate
begin
update @dates set date2=@startdate1 where date2 is null
end
set @startdate1+=1
end
end
else
begin
update @dates set date2=@startdate1-2 where date2 is null
end
set @startdate1+=1
end
select * from
(select date1,date2, rate from #tmpdates
union
select *,0 as rate from @dates
) A WHERE date1>=@startdate and date2<=@enddate
drop table #tmpdates