大哥:UPDATE问题:用X表的NAME列、VALUE列更新Y表(X表、Y表和结果如下)
该SQL语句应如何写?说明:表X的COLUMN列指明了应更新表Y的什么列
例如:第一行的COLUMN列为m1,则说明了要更新Y表的M1列和Q1列,即用表X的VALUE列更新Y表的M1列,用NAME列更新Y表的Q1列(条件是两表的DATE、START、END相等)
表X
DATE   START     END        NAME     VALUE  COLUMN 
---------------------------------------------------
200507   广州   上海       张数      15703    m1 
200507   广州   上海       金额    1268975    m2 
200507   广州   上海       差价        800    m3 
200507   广州   上海       代理费    84895    m4 表Y
DATE    START   END   M1  M2  M3  M4  M5  M6  Q1  Q2  Q3  Q4  Q5  Q6
--------------------------------------------------------------------
200507   广州   天津
200507   广州   上海结果: 表Y
DATE    START   END   Q1    M1   Q2     M2   Q3    M3   Q4     M4  Q5  M5  Q6  M6
----------------------------------------------------------------------------------
200507   广州   天津
200507   广州   上海 张数 15703 金额 1268975 差价 800 代理费 84895
谢谢!!!!!!!!!!!!!

解决方案 »

  1.   

    SQL> select * from x;DATE1  START1 END1   NAME1       VALUE1 CO
    ------ ------ ------ -------- --------- --
    200507 广州   上海   张数         15703 m1
    200507 广州   上海   金额       1268975 m2
    200507 广州   上海   差价           800 m5
    200507 广州   上海   代理费       84895 m6SQL> select * from y
      2  /DATE1  START1 END1   M1     M2     M3     M4     M5     M6         Q1     Q2     Q3     Q4     Q5     Q6
    ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
    200507 广州   天津                                           
    200507 广州   上海                     SQL> update y set (m1,m2,m3,m4,m5,m6,q1,q2,q3,q4,q5,q6) =
      2  (select max(decode(column1,'m1',name1)),max(decode(column1,'m2',name1)),max(decode(column1,'m3',name1)),
      3  max(decode(column1,'m4',name1)),max(decode(column1,'m5',name1)),max(decode(column1,'m6',name1)),
      4  max(decode(column1,'m1',value1)),max(decode(column1,'m2',value1)),max(decode(column1,'m3',value1)),
      5  max(decode(column1,'m4',value1)),max(decode(column1,'m5',value1)),max(decode(column1,'m6',value1)) 
      6  from x where x.date1 = y.date1 and x.start1 = y.start1 and x.end1 = y.end1);2 rows updated       SQL> select * from y
      2  /DATE1  START1 END1   M1     M2     M3     M4     M5     M6         Q1      Q2     Q3     Q4     Q5     Q6
    ------ ------ ------ ------ ------ ------ ------ ------ ------ ------ ------- ------ ------ ------ ------
    200507 广州   天津                                          
    200507 广州   上海   张数   金额                 差价   代理费  15703 1268975                  800  84895        
      

  2.   

    如果没有的话还有insert呢
    写个proc判断一下,没有就insertto duanzilin(寻) ( ):
    max(decode(column1,'m1',name1))是如何取出要更新的值的,谢谢
      

  3.   

    看下面的结果就会明白了,取下面每列的最大值就可以取得要更新的值:select decode(column1,'m1',name1) m1,decode(column1,'m2',name1) m2,decode(column1,'m3',name1) m3,
    decode(column1,'m4',name1) m4,decode(column1,'m5',name1) m5,decode(column1,'m6',name1) m6,
    decode(column1,'m1',value1) q1,decode(column1,'m2',value1) q2 ,decode(column1,'m3',value1) q3,
    decode(column1,'m4',value1) q4, decode(column1,'m5',value1) q5,decode(column1,'m6',value1) q6 
    from x SQL> 
      6  /M1     M2     M3  M4  M5     M6         Q1      Q2  Q3  Q4     Q5     Q6
    ------ ------ --- --- ------ ------ ------ ------- --- --- ------ ------
    张数                                 15703                        
           金额                                1268975                
                          差价                                    800 
                                 代理费                                84895
      

  4.   

    x表加1条上海到南京
    SQL> select * from x;DATE1  STAR END1 NAME1    VALUE1 COLU
    ------ ---- ---- ------ -------- ----
    200507 广州 上海 张数      15703 m1
    200507 广州 上海 金额    1268975 m2
    200507 广州 上海 差价        800 m5
    200507 广州 上海 代理费    84895 m6
    200507 广州 南京 张数      56353 m1SQL> select date1,start1,end1 from y;DATE1  STAR END1
    ------ ---- ----
    200507 广州 天津
    200507 广州 上海insert into y 
    (select date1,start1,end1,
    max(decode(column1,'m1',name1)),max(decode(column1,'m2',name1)),max(decode(column1,'m3',name1)),
    max(decode(column1,'m4',name1)),max(decode(column1,'m5',name1)),max(decode(column1,'m6',name1)),
    max(decode(column1,'m1',value1)),max(decode(column1,'m2',value1)),max(decode(column1,'m3',value1)),
    max(decode(column1,'m4',value1)),max(decode(column1,'m5',value1)),max(decode(column1,'m6',value1)) 
    from x where not exists (select 1 from y where x.date1 = y.date1 and x.start1 = y.start1 and x.end1 = y.end1)
    group by date1,start1,end1)SQL> 
      9  /1 row insertedSQL> select * from y;DATE1  STAR END1 M1     M2     M3 M4 M5     M6         Q1      Q2 Q3 Q4     Q5     Q6
    ------ ---- ---- ------ ------ -- -- ------ ------ ------ ------- -- -- ------ ------
    200507 广州 天津                                                               
    200507 广州 上海 张数   金额         差价   代理费  15703 1268975          800  84895
    200507 广州 南京 张数                               56353                      update 语句要改下:
    update y set (m1,m2,m3,m4,m5,m6,q1,q2,q3,q4,q5,q6) =
    (select max(decode(column1,'m1',name1)),max(decode(column1,'m2',name1)),max(decode(column1,'m3',name1)),
    max(decode(column1,'m4',name1)),max(decode(column1,'m5',name1)),max(decode(column1,'m6',name1)),
    max(decode(column1,'m1',value1)),max(decode(column1,'m2',value1)),max(decode(column1,'m3',value1)),
    max(decode(column1,'m4',value1)),max(decode(column1,'m5',value1)),max(decode(column1,'m6',value1)) 
    from x where x.date1 = y.date1 and x.start1 = y.start1 and x.end1 = y.end1)
    where exists (select 1 from y where x.date1 = y.date1 and x.start1 = y.start1 and x.end1 = y.end1);
      

  5.   

    我试了一下,不用MAX好象也得到一样的结果
    想请教一下MAX的作用是什么? 再谢谢!!!!!!!!!!!!!!111
      

  6.   

    不好意思,搞错了,不用MAX会有很多条记录,用MAX只有一条记录,
    还是想请教一下为什么?       无言感激..........
      

  7.   

    create or replace procedure zc.x_update_y_test istype cur is ref cursor;
    cc cur;
    v_record zc.x%rowtype;v_name varchar2(10);
    v_value varchar2(10);v_var varchar2(10);
    v_tmp varchar2(10);begin
      open cc for 'select * from zc.x'; loop
      fetch cc into v_record;
       exit when cc%notfound;
      v_var:=v_record.column1;
      v_name:=v_record.name;
      v_value:=v_record.value1;
      v_tmp:=substrB(v_var,2,1);  execute immediate 'insert into zc.Y(m'||v_tmp||',q'||v_tmp||')values(:1,:2)'
      using v_name,v_value;  end loop;
    end;
      

  8.   

    begin
    zc.x_update_y_test;
    commit;
    end;--sqlplus运行这个执行存储过程
      

  9.   

    当表X(DATE、START、END相同)的记录超过100时, 即column的值由m1到m100时,用什么方法处理这一问题最合理,速度又快呢?
      

  10.   

    TO duanzilin(寻):.
    .
    .
    from x where x.date1 = y.date1 and x.start1 = y.start1 and x.end1 = y.end1);
    这一名不行啊, y.date1 y.start1 y.end1这些列是找不到的,因为 from x 没有y表
    大哥,你运行过,可以吗?
      

  11.   

    我运行过,没问题的
    update y set (...) = (...from x where x.date1 = y.date1)--这里y.date1是 update y这里的y表,明白?最后那个update有点毛病,改正下:update y set (m1,m2,m3,m4,m5,m6,q1,q2,q3,q4,q5,q6) =
    (select max(decode(column1,'m1',name1)),max(decode(column1,'m2',name1)),max(decode(column1,'m3',name1)),
    max(decode(column1,'m4',name1)),max(decode(column1,'m5',name1)),max(decode(column1,'m6',name1)),
    max(decode(column1,'m1',value1)),max(decode(column1,'m2',value1)),max(decode(column1,'m3',value1)),
    max(decode(column1,'m4',value1)),max(decode(column1,'m5',value1)),max(decode(column1,'m6',value1)) 
    from x where x.date1 = y.date1 and x.start1 = y.start1 and x.end1 = y.end1)
    where exists (select 1 from x where x.date1 = y.date1 and x.start1 = y.start1 and x.end1 = y.end1);