不清楚么?
字段   A    B   C    D    E  ,数据都是字符的。其实无所喂什么类型。。
       0   23   4    7   9
       1   0    23   0   7
       2   4    0    0   0
       to  23   0    0   9我希望根据"to"记录的字段值,统计相应"0,1,2"字段值的总和,即如果"to"的B字段>0,则"0,1,2"的B字段求和23+0+4=27,如果"to"的C字段为0,则给个0值。 "to"的E字段>0,则
"0,1.2"的E字段求和9+7+0=16;  
最后得到一个象这样的记录:
A   B   C  D   E
TT  27  0  0   16可做到么??

解决方案 »

  1.   

    select 
    case when t1.b=0 then 0 when t1.b<>0 then t2.sb end as BB,
    case when t1.c=0 then 0 when t1.c<>0 then t2.sc end as CC,
    case when t1.d=0 then 0 when t1.d<>0 then t2.sd end as DD,
    case when t1.e=0 then 0 when t1.e<>0 then t2.se end as EE
    from
    (select * from t where a='to') t1,
    (select sum(b) as sb,sum(c) as sc,sum(d) as sd,sum(e) as se
     from t where a<>'to') t2
    /
      

  2.   

    select tt,sum(decode((select B from table_name where a='to'),0,0,B),
              sum(decode((select C from table_name where a='to'),0,0,C),
              sum(decode((select D from table_name where a='to'),0,0,D)
    from table_name where a<>'to' group by a