derby sql 两个聚合值之间的差异或如何组合两个 sql 语句
derby sql difference between two aggregate values or how to combine two sql statements
以下两个查询自己工作:
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。第一次合并returns 200000.00,第二次合并returns 0.00
您可以使用 SYSDUMMY1
,一个带有一条记录的虚拟 table,可用于 select 简单值,在这种情况下,两个总和之间的差值:
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
以下两个查询自己工作:
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。第一次合并returns 200000.00,第二次合并returns 0.00
您可以使用 SYSDUMMY1
,一个带有一条记录的虚拟 table,可用于 select 简单值,在这种情况下,两个总和之间的差值:
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