【问题标题】:Missing Dates between two dates in sql [duplicate]sql中两个日期之间缺少日期[重复]
【发布时间】:2019-05-01 02:12:51
【问题描述】:

我想查找两个日期之间缺少的日期。 在表中有一个名为 date 的字段,因为它存在

2018-11-13、2018-11-18、2018-11-20、2018-11-25

我想要从 2018-11-13 到 2018-11-25 的缺失日期

Expected Output
2018-11-14
2018-11-15
2018-11-16
2018-11-17
2018-11-19
2018-11-21
2018-11-22
2018-11-23
2018-11-24

【问题讨论】:

  • 您有任何日历日期表来找出日期范围之间缺失的日期吗?
  • @Avi no sir.....
  • @VenkataramanR 它显示了日期之间的所有缺失天数...我想要在 2018-11-13 和 2018-12-01 之间有一个日期 2018-11-18 存在于数据库查询将跳过该日期并显示剩余的缺失日期
  • @Lima,您能否提供您的数据库架构并更新您的问题。当前的问题具有误导性。
  • @Squirrel 它不会重复它的另一个问题,因为缺失日期不能在表中

标签: sql sql-server


【解决方案1】:

如果您想在某个表中存储缺失的日期,可以使用 While 循环。这样,它也将起作用。

Declare @mindate date ='2018-11-13' 
Declare @maxdate date = '2018-12-01' 

select '2018-11-18' as Existingdate into #tmp  --made existing dates 
create table Missingdates (Missingdate date) 

Declare @id int = 1 
While  dateadd(day, @id, @mindate) > @mindate and dateadd(day, @id, @mindate) < @maxdate  
Begin 
insert into Missingdates  select dateadd(day, @id, @mindate) as MIssingDate  where not exists (Select  * from #tmp t where t.Existingdate = dateadd(day, @id, @mindate)) 
set @id = @id+1 
end 

select * from Missingdates 

输出:

Missingdate
2018-11-14
2018-11-15
2018-11-16
2018-11-17
2018-11-19
-- so on 

添加了 not exists 子句,它不会给出现有日期。

【讨论】:

  • 它显示日期之间的所有缺失天数...我想在 2018-11-13 和 2018-12-01 之间有一个日期 2018-11-18 存在于数据库中查询将跳过该日期并显示其余缺少的日期
  • @Lima 此查询将检查日期是否存在于表中,然后插入缺失的日期。这不会给出 2018-11-18,并返回所有缺失的日期。
【解决方案2】:

这肯定会奏效:

        select * from date_ranges;
        13-NOV-18
        01-DEC-18
        16-NOV-18
        20-NOV-18

        set serveroutput on;
create or replace procedure date_range( d1 date, d2 date) is 
op date;
op2 date;
diff number;
comp date;
a  number;
b number;
j number;
cursor c1 is select a from date_ranges;
begin
select d2-d1 into diff from dual;
for op in 1..diff 
loop <<forloop>>
j:=0;
select d1+op into op2 from dual;

          open c1;
          loop <<curloop>>
          fetch c1 into comp;
          if(c1%notfound) then
          exit;
          end if;
          select sysdate-to_date(comp) into a from dual;
          select sysdate-to_date(op2)  into b from dual;
          if(a=b)then 
          j:=1;
          end if;

          end loop  curloop;

          close c1;
if(j=0)then 
DBMS_OUTPUT.PUT_LINE(op2);
end if;
end loop forloop;
end;
/

declare
m date;
n date;
begin 
select min(a) into m from date_ranges;
select max(a)  into n from date_ranges;
date_range(m,n);
end;
/

输出:

14-NOV-18
15-NOV-18
17-NOV-18
18-NOV-18
19-NOV-18
21-NOV-18
22-NOV-18
23-NOV-18
24-NOV-18
25-NOV-18
26-NOV-18
27-NOV-18
28-NOV-18
29-NOV-18
30-NOV-18

因此日期 11 月 16 日和 11 月 20 日不会打印,因为它们存在于查询表中,并且缺少在样本日期的最小值和最大值之间打印的日期。 谢谢!!!!!!!!!!!!!!!1

【讨论】:

  • 它显示日期之间的所有缺失天数...我想在 2018-11-13 和 2018-12-01 之间有一个日期 2018-11-18 存在于数据库中查询将跳过该日期并显示其余缺少的日期
  • 确定...,,...
  • @Lima 正确吗?这是你想要的!!!!
猜你喜欢
  • 1970-01-01
  • 2019-07-20
  • 1970-01-01
  • 1970-01-01
  • 2015-03-11
  • 2014-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多