如何根据某天:testDay(格式2008-07-08) 
计算出: 
本周(testDay所在的周)的周一(this_week_beg)和周末(this_week_end), 
上周的周一(last_week_beg)和周末(last_week_end ), 
上上周的周一(two_week_beg)和周末( two_week_end), 
上上上周的周一(three_week_beg)和周末(three_week_end), 
本月开始(this_month_beg)和结束( this_month_end), 
上月开始(last_month_beg)和结束(last_month_end). 
谢谢了.... 

解决方案 »

  1.   

    以下全部以同一为开始,周五算周末本周(testDay所在的周)的周一(this_week_beg)和周末(this_week_end), 
    decode(to_char(testday,'day'),'1',testday-6,testday-to_number(to_char(testday,'day'))+1)  本周一
    case
    when to_char(testday,'day')='6'
    then to_char(testday,'day')='7' then  testday-1
    esle testday+6-to_number(to_char(testday,'day'))
    end  本周五上周的周一(last_week_beg)和周末(last_week_end ),
    算出本周的然后-7
     
    上上周的周一(two_week_beg)和周末( two_week_end), 
    -14上上上周的周一(three_week_beg)和周末(three_week_end), 
    -21本月开始(this_month_beg)和结束( this_month_end), 
    trunc(testday,'mm') 本月开始
    last_day(testday) 本月结束
     
    上月开始(last_month_beg)和结束(last_month_end).
    add_months(trunc(testday,'mm'),-1)上月开始last_day(add_months(testday,-1)) 上月结束
      

  2.   

    to_char(testday,'day') 是取该日是一周的第几天
    周日是第一天,周一是第2天,周六是第7天
    我们一般的习惯是以周日为最后天的,和数据库有所不同啊
    我们这的本周日,在数据库里是下一周的开始(西方的习惯)
      

  3.   

    我不明白"decode(to_char(testday,'day'),'1',testday-6,testday-to_number(to_char(testday,'day'))+1)"
    ==>"decode(to_char(2008-07-08,'day'),'1',2008-07-08-6,2008-07-08-to_number(to_char(testday,'day'))+1)"?
      

  4.   

    应该是decode(to_char(testday,'day'),'1',testday-6,testday-to_number(to_char(testday,'day'))+2)" 这句话的意思
    判断
    to_char(testday,'day') 的值
    如果是1,说明testday是周日,那取周一就是testday减去6天
    不是的话,就取testday-to_char(testday,'day') 的值再加2天就是周一
    比如8.25是周一
    to_char(testday,'day')=2
    8.25-2+2=8.25
    如果是8.26
    就是8.26-3+2=8.25
    今天是
    8.27
    to_char(8.27,'day')=4
    8.27-4+2=8.258.24是周日,如果还按这个公式算
    8.24-1+2=8.25按我们的习惯8.24不是本周的,算上周的,所以这时就要换个公式
    8.24-6=8.18 就是上周一