日期:2014-05-17  浏览次数:20866 次

oracle存储过程
写一存储过程做统计,涉及几张表,基础信息表,存放需要统计的地区信息,例如北京、天津、上海

需要处理的逻辑是,循环基础信息表,按照对应的地区名称统计tablea里的这个月,上个月、去年这月的数据。

出现的问题是基础信息表只有一家公司数据正确,多家公司统计出来的ASUM变成了集合。每个地区的数据都一样。

cursor bt is
    select distinct (companycnname) from BASETOTAL;
begin
  v_Date           := to_DATE(strDate, 'yyyy-mm');
  v_lastMonthDate  := add_months(v_Date, -1);
  v_lastYearDate   := add_months(v_Date, -12);
  strDat           := to_char(v_Date, 'yyyy-mm');
  strLastMonthDate := to_char(v_lastMonthDate, 'yyyy-mm');
  strLastYearDate  := to_char(v_lastYearDate, 'yyyy-mm');

  open bt;

  LOOP
    fetch bt
      into COMPANYCNNAME;
      exit when bt%notfound;

    dbms_output.PUT_LINE('COMPANYCNNAME=' || COMPANYCNNAME)
    while i <= 2 loop
        If (i = 0) then
          v_CREATETIME := strDat;
          dbms_output.PUT_LINE(v_CREATETIME);
        else
          if (i = 1) then
            v_CREATETIME := strLastMonthDate;
            dbms_output.PUT_LINE(v_CREATETIME);
          else
            if (i = 2) then
              v_CREATETIME := strLastYearDate;
              dbms_output.PUT_LINE(v_CREATETIME);
            end if;
          end if;
        end If;
     select COUNT(*)
          into ASUM form table1  WHERE to_char(t.CREATETIME, 'yyyy-mm') >= v_CREATETIME
           AND T.ENABLE = 'T' AND SUBSTR(T.no, 2, 2) in
               (select b.extno
                  from BASETOTAL b
                 where b.companycnname = COMPANYCNNAME);
      dbms_output.PUT_LINE('ASUM =' || ASUM );
      i := i + 1;
      END loop;

commit;
end LOOP;


------解决方案------------