表A有两列a,b
表A数据如下:
a   b
10  20
30  10请问用一个什么函数SQL语句实现得到下面两列:
a:
if b>a then
b-a=20-10=10
end if
b:
if b<a then
b-a=10-30=-20
end if
得到结果两例:
10   -20

解决方案 »

  1.   

    直接select b-a from A 不行么?
      

  2.   

    是这样吗?
    SQL> select * from test;         A          B
    ---------- ----------
            10         20
            30         10
            55         10
            10         55
            30         10
            20         306 rows selected.SQL> select max(case when b > a then b - a else null end) as a,min(case when b > a then a - b else b - a end) as b from test group by round(rownum/2);         A          B
    ---------- ----------
            10        -20
            45        -45
            10        -20SQL> 
      

  3.   

    LZ速学
    Selct......case when....
      

  4.   

    select (case when b>a then b-a else a-b end a),(case when b>a then b-a else a-b end b) from table a
      

  5.   

    SQL> with aa as(
      2  select 10 a,20 b from dual union all
      3  select 30,10 from dual)
      4  select sum(case  when b-a>0 then b-a end) t1,sum(case when b-a<=0 then b-a end) t2 from aa;        T1         T2
    ---------- ----------
            10        -20