有一批数据,要求始终保持一个月数据,并且每个月月末数据始终保留。
字段时间  字段其他
10.1
...
10.27
...
10.31
11.1
11.2
....
11.30
12.1
...
今天10.27,保留10.1到10.27数据。
到10.31,保留10.1到10.31数据。
11.1,保留10月数据和当天数据。
11.2,删除10.1数据,其他保留。
11.30,删除10月数据,但要保留10.31数据。
12.1,删除10月数据,保留10.31数据。
12.2,删除10月数据及11.1数据,保留10.31数据及其他数据。
急请用SQL写出。

解决方案 »

  1.   

    我写一个日期字段为 date 类型的例子
    create table test_date(sdate        date);insert into test_date
    select trunc(sysdate,'yyyy') + rownum - 1 from dual
    connect by rownum <= to_number(to_char(sysdate,'ddd'));
    commit;select * from test_date;delete from test_date
     where trunc(sdate) <= trunc(add_months(sysdate, -1) -1) 
       and trunc(sdate) <> trunc(last_day(sdate));
    commit;select * from test_date;
      

  2.   

    日期字段是Number型的,20111027。
      

  3.   

    你的数据是日期分区了么?可以用last_date+1的方式处理 select last_day(sysdate) from dual
      

  4.   

    upand trunc(sdate) <> trunc(last_day(sdate));
    这句话就是保留月末最后一天。
      

  5.   

    create table test_date(sdate        date);insert into test_date
    select trunc(sysdate,'yyyy') + rownum - 1 from dual
    connect by rownum <= to_number(to_char(sysdate,'ddd'));
    commit;select * from test_date;delete from test_date
     where trunc(sdate) <= trunc(add_months(sysdate, -1) -1) 
       and trunc(sdate) <> trunc(last_day(sdate));
    commit;select * from test_date;--PASS!
      

  6.   

    and trunc(sdate) <> trunc(last_day(sdate));
    这个不就是过滤掉月末不删除吗?
    你实际的把我的例子执行一下,也能看出来是保留的呀
      

  7.   

    2011-1-1
    2011-1-2
    2011-1-3
    2011-1-4
    2011-1-5
    2011-1-6
    2011-1-7
    2011-1-8
    2011-1-9
    2011-1-10
    2011-1-11
    2011-1-12
    2011-1-13
    2011-1-14
    2011-1-15
    2011-1-16
    2011-1-17
    2011-1-18
    2011-1-19
    2011-1-20
    2011-1-21
    2011-1-22
    2011-1-23
    2011-1-24
    2011-1-25
    2011-1-26
    2011-1-27
    2011-1-28
    2011-1-29
    2011-1-30
    2011-1-31
    2011-2-1
    2011-2-2
    2011-2-3
    2011-2-4
    2011-2-5
    2011-2-6
    2011-2-7
    2011-2-8
    2011-2-9
    2011-2-10
    2011-2-11
    2011-2-12
    2011-2-13
    2011-2-14
    2011-2-15
    2011-2-16
    2011-2-17
    2011-2-18
    2011-2-19
    2011-2-20
    2011-2-21
    2011-2-22
    2011-2-23
    2011-2-24
    2011-2-25
    2011-2-26
    2011-2-27
    2011-2-28
    2011-3-1
    2011-3-2
    2011-3-3
    2011-3-4
    2011-3-5
    2011-3-6
    2011-3-7
    2011-3-8
    2011-3-9
    2011-3-10
    2011-3-11
    2011-3-12
    2011-3-13
    2011-3-14
    2011-3-15
    2011-3-16
    2011-3-17
    2011-3-18
    2011-3-19
    2011-3-20
    2011-3-21
    2011-3-22
    2011-3-23
    2011-3-24
    2011-3-25
    2011-3-26
    2011-3-27
    2011-3-28
    2011-3-29
    2011-3-30
    2011-3-31
    2011-4-1
    2011-4-2
    2011-4-3
    2011-4-4
    2011-4-5
    2011-4-6
    2011-4-7
    2011-4-8
    2011-4-9
    2011-4-10
    2011-4-11
    2011-4-12
    2011-4-13
    2011-4-14
    2011-4-15
    2011-4-16
    2011-4-17
    2011-4-18
    2011-4-19
    2011-4-20
    2011-4-21
    2011-4-22
    2011-4-23
    2011-4-24
    2011-4-25
    2011-4-26
    2011-4-27
    2011-4-28
    2011-4-29
    2011-4-30
    2011-5-1
    2011-5-2
    2011-5-3
    2011-5-4
    2011-5-5
    2011-5-6
    2011-5-7
    2011-5-8
    2011-5-9
    2011-5-10
    2011-5-11
    2011-5-12
    2011-5-13
    2011-5-14
    2011-5-15
    2011-5-16
    2011-5-17
    2011-5-18
    2011-5-19
    2011-5-20
    2011-5-21
    2011-5-22
    2011-5-23
    2011-5-24
    2011-5-25
    2011-5-26
    2011-5-27
    2011-5-28
    2011-5-29
    2011-5-30
    2011-5-31
    2011-6-1
    2011-6-2
    2011-6-3
    2011-6-4
    2011-6-5
    2011-6-6
    2011-6-7
    2011-6-8
    2011-6-9
    2011-6-10
    2011-6-11
    2011-6-12
    2011-6-13
    2011-6-14
    2011-6-15
    2011-6-16
    2011-6-17
    2011-6-18
    2011-6-19
    2011-6-20
    2011-6-21
    2011-6-22
    2011-6-23
    2011-6-24
    2011-6-25
    2011-6-26
    2011-6-27
    2011-6-28
    2011-6-29
    2011-6-30
    2011-7-1
    2011-7-2
    2011-7-3
    2011-7-4
    2011-7-5
    2011-7-6
    2011-7-7
    2011-7-8
    2011-7-9
    2011-7-10
    2011-7-11
    2011-7-12
    2011-7-13
    2011-7-14
    2011-7-15
    2011-7-16
    2011-7-17
    2011-7-18
    2011-7-19
    2011-7-20
    2011-7-21
    2011-7-22
    2011-7-23
    2011-7-24
    2011-7-25
    2011-7-26
    2011-7-27
    2011-7-28
    2011-7-29
    2011-7-30
    2011-7-31
    2011-8-1
    2011-8-2
    2011-8-3
    2011-8-4
    2011-8-5
    2011-8-6
    2011-8-7
    2011-8-8
    2011-8-9
    2011-8-10
    2011-8-11
    2011-8-12
    2011-8-13
    2011-8-14
    2011-8-15
    2011-8-16
    2011-8-17
    2011-8-18
    2011-8-19
    2011-8-20
    2011-8-21
    2011-8-22
    2011-8-23
    2011-8-24
    2011-8-25
    2011-8-26
    2011-8-27
    2011-8-28
    2011-8-29
    2011-8-30
    2011-8-31
    2011-9-1
    2011-9-2
    2011-9-3
    2011-9-4
    2011-9-5
    2011-9-6
    2011-9-7
    2011-9-8
    2011-9-9
    2011-9-10
    2011-9-11
    2011-9-12
    2011-9-13
    2011-9-14
    2011-9-15
    2011-9-16
    2011-9-17
    2011-9-18
    2011-9-19
    2011-9-20
    2011-9-21
    2011-9-22
    2011-9-23
    2011-9-24
    2011-9-25
    2011-9-26
    2011-9-27
    2011-9-28
    2011-9-29
    2011-9-30
    2011-10-1
    2011-10-2
    2011-10-3
    2011-10-4
    2011-10-5
    2011-10-6
    2011-10-7
    2011-10-8
    2011-10-9
    2011-10-10
    2011-10-11
    2011-10-12
    2011-10-13
    2011-10-14
    2011-10-15
    2011-10-16
    2011-10-17
    2011-10-18
    2011-10-19
    2011-10-20
    2011-10-21
    2011-10-22
    2011-10-23
    2011-10-24
    2011-10-25
    2011-10-26
    2011-10-27--SQL:
    create table test_date(sdate        date);insert into test_date
    select trunc(sysdate,'yyyy') + rownum - 1 from dual
    connect by rownum <= to_number(to_char(sysdate,'ddd'));
    commit;delete from test_date
     where trunc(sdate) <= trunc(add_months(sysdate, -1) -1) 
       and trunc(sdate) <> trunc(last_day(sdate));
    commit;
    --result:2011-1-31
    2011-2-28
    2011-3-31
    2011-4-30
    2011-5-31
    2011-6-30
    2011-7-31
    2011-8-31
    2011-9-27
    2011-9-28
    2011-9-29
    2011-9-30
    2011-10-1
    2011-10-2
    2011-10-3
    2011-10-4
    2011-10-5
    2011-10-6
    2011-10-7
    2011-10-8
    2011-10-9
    2011-10-10
    2011-10-11
    2011-10-12
    2011-10-13
    2011-10-14
    2011-10-15
    2011-10-16
    2011-10-17
    2011-10-18
    2011-10-19
    2011-10-20
    2011-10-21
    2011-10-22
    2011-10-23
    2011-10-24
    2011-10-25
    2011-10-26
    2011-10-27