select type,decode(type,1,a1,a2) a,decode(type,1,b1,b2) b from tname;

解决方案 »

  1.   

    select type,decode(type,1,a1,a2) a,decode(type,1,b1,b2) b from xxx
      

  2.   

    select type,decode(type,1,A1,A2) A,decode(type,1,B1,B2) B
    from table_name
      

  3.   

    type是oracle的关键字,尽量不要用来做列名
      

  4.   

    一会儿就要了三条一样的答案.
    如果你掌握了decode,SQL语句的支持至少会了一半儿,另一半儿是Where条件。
      

  5.   

    select type,A1 A,B1 B from table where type = 1
    union
    select type,A2 A,B2 B from table where type <> 1
      

  6.   

    你的oracle版本如果是8.1以上,也可以这样:
    select type,(case type when 1 then A1 else B1 end) as A,(case type when 1 then B1 else B2 end) as B from tablename
      

  7.   

    select type ,decode(type,'1',A1,'2',A2,'3',A2),DECODE(TYPE,'1',B1,'2',B2,'3',B2)
    根据你给的样本,我觉的这样可以,你可以试试
      

  8.   

    多谢
    我加60分再问一个小问题:
    加入我的A1,A2,B1,B2的字段都是int类型,可否select到根据此表的另外一个字段id汇总的结果?比如如下:
    id  type  A1  A2  B1  B2
    10  1     11  22  11  22
    11  2     1   2   1   2
    11  1     8   10  8   10
    10  1     8   10  8   10
    SELECT如下结果:(即对我第一个问题的结果再对A,B汇总,此时type字段不用select)
    id    A    B
    10    19   19   (=11+8即为楼上的结果再针对一个字段进行汇总)
    11    10   10   (=2+8,注意type1选A1,B1,type2选A2,B2) 哪位大虾能够再帮我整理一下这个select语句?
    我加分  
      

  9.   

    将这个SQL在改一下即可:
    select type ,decode(type,'1',A1,'2',A2,'3',A2),DECODE(TYPE,'1',B1,'2',B2,'3',B2)
    改为:
    select ID ,SUM(decode(type,'1',A1,'2',A2,'3',A2)),SUM(DECODE(TYPE,'1',B1,'2',B2,'3',B2)) FROM TABLE GROUP BY ID
    我只能给你个提示,在我这里没办法调试,你可参考修改,修改,用这种方法是可以实现的,常用,试试吧。
      

  10.   

    你这种方法我自个想过了,调试的时候提示未找到关键字
    应该是id字段的缘故
    我自个调试如果添加where id = 10;便可,但是这样只能统计一个id的数据,我需要的是统计所有id的所有汇总阿
    没有大虾进来么?
      

  11.   

    select id,sum(decode(type,1,a1,a2)) a,sum(decode(type,1,b1,b2)) b from tname group by id;
      

  12.   

    我的语句调试过,正确。
    fanzhaoyou(fanzhaoyou)的语句我也试过,也是正确的。
    会不会与版本有关,我的是8.1.7,你的呢?