通过存储过程找到下面的数据姓名    职位    上班    培训    标准     年终奖
--------------------------------------------------
张三    经理    180天   30天    5000      4750 
李四    主管    220天           4000      4000
王五    主管    190天           4000      3800
阿大    科长    110天           3000      1650  
阿大    员工    160天           2000      1600
业务是这样的,满勤是200天,大于200天按200天算 然后根据标准去算年终奖 培训的话只能拿一般的钱
我写的存储过程只能算出上面的数据  其实是不对的
阿大的年终奖应该是2550
===================================================
阿大    科长    110天           3000      1650  
阿大    员工    160天           2000      900
====================================================先取科长的110天 再取员工的90天
可是因为这些记录是group by出来的 在不同的记录里 
那160天取90天应该是(200减去前一条记录中的110得出来的),
也就是说要去判断相邻的记录的天数,
存储过程中怎么去实现这样的操作呢?

解决方案 »

  1.   

    你写的意思我没看懂,但是有LEAD 分析函数可以去相邻的记录
      

  2.   

    楼主贴出原数据和想要得到的数据可以更快帮助解决此问题,
    楼上的说的对:试试leg以及lead分析函数
      

  3.   

    create table emptest(name varchar2(10),job varchar2(10),worktime number,traintime number,std number,yearend number);
    insert into emptest(name,job,worktime,traintime,std)
    select '张三','经理',180,30,5000 from dual union all
    select '李四','主管',220,null,4000 from dual union all
    select '王五','主管',190,null,4000 from dual union all
    select '阿大','科长',110,null,3000 from dual union all
    select '阿大','员工',160,null,2000 from dual;
    declare
    v_name varchar2(10);
    v_t number;
    cursor cur is select * from emptest order by name,std desc for update;
    begin
    for cur1 in cur loop
    if cur1.name=v_name then
      update emptest set yearend=case when 200-v_t<worktime then 200-v_t else worktime end/200*std
      where current of cur;
    v_t:=case when v_t+cur1.worktime>200 then 200 else v_t+cur1.worktime end;
    else
    v_t:=case when cur1.worktime>200 then 200 else cur1.worktime end;
      update emptest set yearend=v_t/200*cur1.std
      where current of cur;
    v_name:=cur1.name;
    end if;
    end loop;
    end;select * from emptest;NAME JOB WORKTIME TRAINTIME STD YEAREND
    张三 经理 180 30 5000 4500
    李四 主管 220 4000 4000
    王五 主管 190 4000 3800
    阿大 科长 110 3000 1650
    阿大 员工 160 2000 900select name,sum(yearend) from emptest group by name;NAME SUM(YEAREND)
    阿大 2550
    王五 3800
    李四 4000
    张三 4500
      

  4.   

    试试这个
    select t.*,case when s<200 then worktime/200*std 
      when lag(s)over(partition by name order by std desc) is null then std
      when lag(s)over(partition by name order by std desc)<200 then (200-lag(s)over(partition by name order by std desc))/200*std
      else 0 end yearend
    from(
    select name,job,worktime,traintime,std,
      sum(worktime)over(partition by name order by std desc)s from emptest)tNAME JOB WORKTIME TRAINTIME STD S YEAREND
    阿大 科长 110 3000 110 1650
    阿大 员工 160 2000 270 900
    李四 主管 220 4000 220 4000
    王五 主管 190 4000 190 3800
    张三 经理 180 30 5000 180 4500