有张表TABLE
TABLE里包含ID、A、B这几列
现要求C,当A<=B时,C=0;当A>B时,C=A-B
结果表如下
ID      C这个SQL语句怎么写??注意表中没有C这一列

解决方案 »

  1.   


    select ID,decode(A<=B,0,A-B) C from 表
      

  2.   


    --這樣用吧,1有錯
    select id,(case when a<=b then 0 else a-b end) c from t;
      

  3.   


    select t.id,case when t.a<=t.b then 0 else t.a-t.b end
    from table1 t
      

  4.   

    select ID,
    A,
    B,
    decode(sign(A - B), 1, A - B, '0')
    from TABLE1
      

  5.   


    呃,我发现我的问题没有这么简单了,事实上我的A、B列是通过查询其他表得出的结果
    (select ……) A, (select ……) B
    这个时候再求C,这个SQL该怎么写呢?
      

  6.   

    select id,(case when a<=b then 0 else a-b end) c from t;