超过180天的
select *
from tab_a
where trunc(入库日期-sysdate)>180
“如果满100万”这个条件,则涉及判断,应该不能一条SQL可以表达的吧,学习

解决方案 »

  1.   

    写个存储过程,从当天开始向前循环计算。直到180天。如果在此之前累计数达到100万,那就是这一天之前的记录取出来。
    用一个语句的话,用exists应该也可以,但是性能可能有问题。
      

  2.   

    条件真的比较多,,一个SQL真的很难,就是整出来了,也不易理解,所以用存储过程吧。可以先把超过180天的记录找出来。select id from a where reg_time-sysdate>180;
    然后DELETE掉这些,然后在看一下还是否够新装来的罐头的容量,不够的话在去销毁。。
      

  3.   

    简单的做法:创建一个触发器,在插入新记录(新罐头生产出来)的时候,检查仓库中是否存在180天以前的罐头,存在则删除,再检查仓库是否已经满了,如果满了则删除一条最旧的记录;create or replace trigger gan_insert_trigger
      before insert on test_gan  
      for each row
    declare
      v_count number default 0;
    begin
     if inserting  then 
        delete from test_gan where trunc(sysdate) - InDate >= 180 ;
        select count(*) into v_count from test_gan ;
        if v_count >= 1000000 then 
           delete from test_gan where rowid in (
             select rowid from 
               (select row_number() over(order by Indate asc) as rid from test_gan)
             where rid between 1 and (v_count-(1000000-1)));
        end if;   
     end if;
     
    end gan_insert_trigger;