工艺序号 投入量 转换率 应产出量          应产出量的说明(此处为应产出量的算法)
1 20 1 20          (20*1)
2 0 2 40          (20*1*2)
3 0 1 40          (20*1*2*1)
4 10 2 80+20=100          (20*1*2*1*2)+(10*2)
5 0 3 240+60=300        (20*1*2*1*2*3)+(10*2*3)
6 5 2 240+120+10=370 (20*1*2*1*2*3*2)+(10*2*3*2)+(5*2)“应产出量”那一列,我只是要数值型的结果,只是为了把问题描述得更清楚,所以把公式写上去了
请一条SQL搞定谢谢各位哈

解决方案 »

  1.   

    这个还不是很清楚,我知道sql server 可以直接用as,但是Oracle不行呢。……期待……
      

  2.   

    create table tablename
    (
        ……   ……
        应出产量 as ((20*1*2*1*2*3*2)+(10*2*3*2)+(5*2))
    )
    或者如果是其他的公式也行,直接换上就可以了。。写在括号里面
      

  3.   

    这个用程序写或油标来做,比较简单,直接用SQL语句来做,很麻烦
      

  4.   

    这个用存储过程比较容易实现
    测试表结构
    create table t1(id integer,input number,rate number,output number,re varchar2(4000));
    insert into t1(id,input,rate) 
    select 1,20,1 from dual
    union all select 2,0,2 from dual
    union all select 3,0,1 from dual
    union all select 4,10,2 from dual
    union all select 5,0,3 from dual
    union all select 6,5,2 from dual;过程
    declare
    type t1 is table of number index by binary_integer;
    input_array t1;
    rate_array t1;
    type t2 is table of varchar2(4000) index by binary_integer;
    rate_array_tmp t2;
    cursor c1 is select * from t1 order by id for update;
    v_output number:=0;
    v_rate varchar2(2000);
    v_re varchar2(4000);
    begin
      for x in c1 loop
        v_output:=(v_output+x.input)*x.rate;
        v_rate:=v_rate||'*'||to_char(x.rate);
        input_array(x.id):=x.input;
        rate_array(x.id):=x.rate;
        v_re:=null;
        for i1 in input_array.first .. x.id loop
          v_rate:=null;
          for i2 in i1 .. x.id loop
            v_rate:=v_rate||'*'||rate_array(i2);
          end loop;
          rate_array_tmp(i1):=case when input_array(i1)=0 then null else '('||to_char(input_array(i1))||v_rate||')' end;
        end loop;
        v_re:=null;
        for i3 in input_array.first .. x.id loop
          v_rate:=rate_array_tmp(i3);
          v_re:=v_re||case when v_re is not null and v_rate is not null then '+' end||v_rate;
        end loop;
        --dbms_output.put_line(re_array(x.id));
        update t1 set t1.output=v_output,t1.re=v_re where current of c1;
      end loop;
      commit;
    end;
      

  5.   

    方法2,用sql语句来拼出re,然后写一个函数compute_value来计算出该re的值,即output
    create or replace function compute_value(str in varchar2)
    return number
    as
    v_value number;
    begin
      execute immediate 'select '||str||' from dual' into v_value;
      return v_value;
      exception
        when others then
          dbms_output.put_line(sqlerrm);
          return null;
    end;with tmp1 as
     (select connect_by_root input input,
             id,
             sys_connect_by_path(rate, '*') rate
        from t1 x
       start with x.input != 0
      connect by id = prior id + 1)
    select id, input, rate, compute_value(re) output, re
      from(
    select id, input, rate,(
    select replace(cast(wm_concat('(' || t2.input || t2.rate || ')') as varchar2 (1000)), ',', '+')
      from tmp1 t2
     where id = t1.id) re
      from t1)
    更新:
    update t1 set re=
    (select replace(cast(wm_concat('(' || t2.input || t2.rate || ')') as varchar2 (1000)), ',', '+') re
      from (select connect_by_root id id1,
             connect_by_root input input,
             id,
             sys_connect_by_path(rate, '*') rate
        from t1 x
       start with x.input != 0
      connect by id = prior id + 1) t2
     where id = t1.id);update t1 set output=compute_value(re);
    commit;