表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
表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
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>
Selct......case when....
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