db

declare
   i integer;
   type my_type is record
   (
    v_name varchar2(50),
    v_id   number
   );
   type my_table is table of my_type;
    
   v_person my_type;
   v_table my_table:=my_table();
    
   rowindex integer;
   prev_id number;--前一行ID
   c integer;
begin
   rowindex:=1;
   prev_id:=0;
   c:=0;
   for cc in(select first_name,department_id from employees t
   order by department_id)
   loop
      if rowindex>1 then --不是第一行
         if cc.department_id<>prev_id or
         (cc.department_id is null and prev_id is not null) or
         (cc.department_id is not null and prev_id is null)
         --不一样
         then
            --中间的小计
            --dbms_output.put_line(prev_id||'小计:'||'  '||c);
            v_table.extend();
            v_person.v_name:=prev_id||'小计:';
            v_person.v_id:=c;
            v_table(rowindex):=v_person;--行号从1开始
            rowindex:=rowindex+1;
            c:=0;
         end if;
      end if;   
      prev_id:=cc.department_id;
      c:=c+1;
      --真实的数据
      --dbms_output.put_line(cc.first_name||'  '||cc.department_id);
      v_table.extend();
      v_table(rowindex):=cc; 
      rowindex:=rowindex+1; 
   end loop;
   --最后的小计
   --dbms_output.put_line(prev_id||'小计:'||'  '||c);
   v_table.extend();
   v_person.v_name:=prev_id||'小计:';
   v_person.v_id:=c;
   v_table(rowindex):=v_person; 
     
   dbms_output.put_line('--------------------------------------------------------');
   rowindex:=v_table.first;
   loop
      exit when rowindex is null;
      dbms_output.put_line(rpad(v_table(rowindex).v_name,20,' ')||v_table(rowindex).v_id);
      rowindex:=v_table.next(rowindex);
   end loop;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值