求sql:从4月1号到9月1号每隔3周的星期一星期二的日期

解决方案 »

  1.   

    大晚上的 休息了早上弄 这种类型的sql 貌似碰到的不多
      

  2.   

    还没怎么想 写的比较麻烦 凑合看着了
    --过滤小于4月1号的日期
    select c1,to_char(c1,'ww') "周数",to_char(c1,'d') "星期"
    from
    (
    --得出每个周二和往前推2天的日期
    select distinct c1-level+1 c1
    from
    (
      --根据最小的日期一次往前推3周
      select c1 + 21*(level-1) c1
      from
      (
          --计算为周二的最小日期
          select min(c1) c1
          from 
          (
              --构造4月1号到9月1号的日期表
              select date'2013-04-01'+level-1 c1 
              from dual
              connect by level <= date'2013-09-01' - date'2013-04-01'+1
          )
          where to_char(c1,'d') = 2
      )
      connect by level <= (date'2013-09-01' - date'2013-04-01')/21+1
    )
    connect by level < 4
    order by c1
    )
    where c1 >= date'2013-04-01'
         日期     周数    星期
    ---------------------------------------------
    1 2013/4/1 13 2
    2 2013/4/20 16 7
    3 2013/4/21 16 1
    4 2013/4/22 16 2
    5 2013/5/11 19 7
    6 2013/5/12 19 1
    7 2013/5/13 19 2
    8 2013/6/1 22 7
    9 2013/6/2 22 1
    10 2013/6/3 22 2
    11 2013/6/22 25 7
    12 2013/6/23 25 1
    13 2013/6/24 25 2
    14 2013/7/13 28 7
    15 2013/7/14 28 1
    16 2013/7/15 28 2
    17 2013/8/3 31 7
    18 2013/8/4 31 1
    19 2013/8/5 31 2
    20 2013/8/24 34 7
    21 2013/8/25 34 1
    22 2013/8/26 34 2
      

  3.   

    发现个问题 第四层过滤where to_char(c1,'d') = 2改成where to_char(c1,'d') = 3to_char(c1,'d')这个表示是当周第几天 然后从周日开始计算 也就是周日是1 一次推到周六是7 然后最外层星期可以不写 也可以判断改成
    decode(to_char(c1,'d'),'1','周日','2','周一','3','周二','4','周三','5','周四','6','周五','7','周六')