简体   繁体   English

根据同一表中另一个字段的值获取Mysql编号的净值

[英]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.

相关问题 MySQL根据另一个表中同一行的另一个字段值的重复外观更新布尔值 - MySQL updating a Boolean field value based on the duplicated appearance of another filed value of the same row from another table MySQL - 根据同一个表中的相同字段获取列值的总和 - MySQL - get the sum of the column value(s) based on same filed on the same table 如何从MySql中的另一个字段派生一个字段的值 - how to derived value of a filed from another field in MySql MySQL计数一个字段值出现在另一个表字段中的次数 - mysql count number of times a field value appear in another table field MySQL Group由一个字段基于另一个字段IN THE GROUP的最大值(不在表中) - MySQL Group by a field based on the maximum value of another field IN THE GROUP (not in the table) MySQL - 根据同一表中的另一个行值计算行值 - MySQL - Calculate a row value based on another row value in the same table MySQL更新字段与来自同一表中另一个字段的值 - mysql update field with value from another field in same table MySQL-使用基于另一个表的值填充表字段 - MySQL - populate table field with value based on another table MySQL内部基于字段值连接同一张表 - MySQL inner join same table based on field value MySQL视图将3个字段链接到另一个表中具有不同值的同一字段 - MySQL view linking 3 fields to the same field in another table with a different value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM