如何计算一列中值的差异

时间:2016-07-24 15:51:11

标签: oracle

C1 |C2 |C3
a  |b1 |1
a  |b2 |2
a  |b3 |3
a  |b4 |4
b  |b1 |5
b  |b2 |6
b  |b3 |10
b  |b4 |11

鉴于上面的表数据,我想要输出如下:

C1 |DIFF
a  |3
b  |6

此处,DIFFC3列值的差异,其中C2='b4'C2='b1'。 在Oracle中做这个的逻辑是什么?

1 个答案:

答案 0 :(得分:0)

也许这...... my_table是你的输入表。

select t1.c1 as c1, t1.c3 - t2.c3 as diff
from   (select c1, c3 from my_table where c2 = 'b4') t1
       inner join
       (select c1, c3 from my_table where c2 = 'b1') t2
       on t1.c1 = t2.c1
;