可以用Decode函数啊,如果表一中c2的取值固定的话,比较简单了如果不固定,则通过写存储过程,写动态的SQL语句包含(decode),返回记录集当然还有其他的办法,比如利用临时表,把数据写到临时表,再查询临时表

解决方案 »

  1.   

    你的分数我全要!!!!!!!!!
    最清晰的解法:
    存储过程法:
    定义n个变量va,vb,vc...;
    定义一个变量存储中间标志flag boolean;
    declare 一个CURSOR cur_1 for slect unique c3 from t_1;
    for i in cur_1
    begin
      select i into va from dual where exist (select c2 from t_1 where c3=i) and c2='a';
      select i into vb from dual where exist (select c2 from t_1 where c3=i) and c2='b';
      select i into vc from dual where exist (select c2 from t_1 where c3=i) and c2='c';
    ...
      insert into t_2 values(va,vb,vc...);
    end
    end
    /
    以上只是思想,没有在机器上试过,程序需要仔细调试。
      

  2.   

    不好意思,没看到,己经有人来了,分数随你分吧。
    我估计用一条SQL也可以实现,但难度较大,而且较繁,需要实际测试过,我周一要出差了,可能没空写了。
      

  3.   

    更正一个明显的错误
    declare 一个CURSOR cur_1 for slect unique c3 from t_1;
    for i in cur_1
    begin
      select i into va from dual where exist (select c2 from t_1 where c3=i and c2='a');
      select i into vb from dual where exist (select c2 from t_1 where c3=i and c2='b');
      select i into vc from dual where exist (select c2 from t_1 where c3=i and c2='c');
    ...--可能va,vb,vc...用数组会显得简洁一些
      insert into t_2 values(va,vb,vc...);
    end
    end
    /
      

  4.   

    各位,由于各种限制,临时表、存储过程等不可以用,只能用一条SQL实现,并且可以同时使用SQl server 和 Oracle.
      

  5.   

    Use only one sql is not possible as the value of c2,c3 is not fixed , in other words , the column name of the second table is not fixed too.
    I have seen a lot of simular question like yours . Bascially you should not consider use only one sql
      

  6.   

    非常感谢 black_snail(●○) 和 jiezhi(相逢何必曾相识) 放宽一下条件:
    那么,由外部程序动态构造出一个SQL是否可以完成?
    或者使用一个存储过程完成也可以,只要不使用临时表。
      

  7.   

    可以动态出构造出一个含有decode的SQL.
      

  8.   

    你要做什么的,
    能不能换个思路
    我总觉得这好像已经超出rdbms的基本范畴了