以下两个查询可以自己工作:
select coalesce(sum(shares*cps),0.0) from
transactions
where usr = 1
and (type = 'C'
or type = 'S')
select coalesce(sum(shares*cps),0.0) from
transactions
where usr = 1
and (type = 'W'
or type = 'B')
我如何结合它们以得到它们之间的区别?像这样:
select coalesce(sum(a.shares*a.cps),0.0) - coalesce(sum(b.shares*b.cps),0.0) from
(select * from transactions
where usr = 1
and (type = 'C'
or type = 'S')) as a,
(select * from transactions
where usr = 1
and (type = 'W'
or type = 'B')) as b;
返回的值为0。第一个合并返回200000.00,第二个合并返回0.00
答案 0 :(得分:2)
您可以使用SYSDUMMY1
,这是一个虚拟表,其中有一条记录,可用于选择简单值,在这种情况下,这是两个总和之差:
select
( select coalesce(sum(shares*cps),0.0) from
transactions
where usr = 1
and (type = 'C'
or type = 'S')) -
( select coalesce(sum(shares*cps),0.0) from
transactions
where usr = 1
and (type = 'W'
or type = 'B')) as DIFFERENCE
from SYSIBM.SYSDUMMY1