[英]Get the net value of a Mysql number filed based on the value of another field in the same table
I am using the below query for getting the net value of a number(amount) field in my table. 我正在使用以下查询来获取表中数字(金额)字段的净值。 The net value is calculating based on the type of a field called transaction type.
净值是根据称为交易类型的字段类型进行计算的。 Can anybody help me by advising is there any way to simplify my below query.
任何人都可以通过建议帮助我,有什么方法可以简化我的以下查询。
select (select sum(jvm_txn_amount) from jvm_transactions
where jvm_txn_mode = 'CASH' and
jvm_txn_type = 'Receipt' and
jvm_txn_mnt_stat = 'A') -
(select sum(jvm_txn_amount) from jvm_transactions
where jvm_txn_mode = 'CASH' and
jvm_txn_type = 'Payment' and
jvm_txn_mnt_stat = 'A')as net_balance
from dual
Possibly start by grouping by jvm_txn_type
: 可能首先按
jvm_txn_type
分组:
SELECT SUM(IF(x.type='Receipt', s, 0)) -
SUM(IF(x.type='Payment', s, 0)) AS net_balance FROM
( SELECT jvm_txn_type type, SUM(jvm_txn_amount) s
FROM jvm_transactions
WHERE jvm_txn_mode = 'CASH'
AND jvm_txn_mnt_stat = 'A'
GROUP BY jvm_txn_type
) x
It's somewhat more compact and less repetitive, which ought to make it a little more versatile and easier to maintain. 它更紧凑,更不重复,应该使它更具通用性并且更易于维护。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.