2016-10-03 41 views
0

我想按升序排列日期序列中的所有缺失日期。我怎样才能做到这一点,而不使用任何函数或udfs一个简单的SQL。获取日期序列中的所有缺失日期

Input :- 

2016-09-01 
2016-09-02 
2016-09-05 
2016-09-10 

输出: -

2016-09-03 
2016-09-04 
2016-09-06 
2016-09-07 
2016-09-08 
2016-09-09 

我已经试过?

select start, stop 
    from 
     (
      select m.x + 1 as start, 
       (select min(x) - 1 from X as x where x.x > m.x) as stop 
      from X as m 
      left outer join X as r 
       on m.x = r.x - 1 
      where r.x is null 
     ) as x 
    where stop is not null; 
+0

如果您标记了正在使用的数据库管理系统,也表明你的尝试,这将有助于。 –

+0

我想用整数解决它,然后跳转到日期。 – Teja

+0

你的数据库支持递归公用表表达式吗? –

回答

-1
  1. 创建一个新表,并插入365行编号1-365 (交替使用已经有超过365行的表,并使用ROWNUM或类似的结构来获得独特的整数)

  2. 转换日期为整数(在Oracle使用像TO_CHAR(mydate, 'ddd')

  3. 在查询中联合起来这两个列表中找到适当的一组(重叠,缺失等)

  4. 转换回日期

+0

我正在使用亚马逊红移 – Teja

+0

@Teja我不明白Amazon Redshift支持cte。 –

+0

Amazon Redshift不支持递归类。检查这个链接。 http://docs.aws.amazon.com/redshift/latest/dg/c_unsupported-postgresql-features.html – Teja

相关问题