Sql 如果结果为正数和负数,如何从多个列求和并分离到单独的列中

Sql 如果结果为正数和负数,如何从多个列求和并分离到单独的列中,sql,postgresql,aggregate-functions,Sql,Postgresql,Aggregate Functions,我使用的是postgresql,需要编写一个查询,对两个不同表的独立列中的值求和,然后将其分隔为单独的列(如果为正数或负数) 比如说, 下面是源表 下面是需要创建的结果表,在填充时也会用到它 我在下面写了一个查询,以汇总总额,并能够填充TOT_贷方和TOT_借方列。是否有任何优化的查询来实现这一点 select t.account_id, t.transaction_date, SUM(t.transaction_amt) filter (where t.tra

我使用的是postgresql,需要编写一个查询,对两个不同表的独立列中的值求和,然后将其分隔为单独的列(如果为正数或负数)

比如说,

下面是源表

下面是需要创建的结果表,在填充时也会用到它

我在下面写了一个查询,以汇总总额,并能够填充TOT_贷方和TOT_借方列。是否有任何优化的查询来实现这一点

select  t.account_id,
        t.transaction_date,
        SUM(t.transaction_amt) filter (where t.transaction_amt >= 0) as tot_debit,
        SUM(t.transaction_amt) filter (where t.transaction_amt < 0) as tot_credit,
        case
        when
        (
            SUM(t.transaction_amt) + 
            SUM(COALESCE(b.credit_balance,0)) +
            SUM(COALESCE(b.debit_balance,0))
        ) < 0
        then
        (
            SUM(t.transaction_amt) + 
            SUM(COALESCE(b.credit_balance,0)) +
            SUM(COALESCE(b.debit_balance,0))
        )
        end as credit_balance,
        case
        when
        (
            SUM(t.transaction_amt) + 
            SUM(COALESCE(b.credit_balance,0)) +
            SUM(COALESCE(b.debit_balance,0))
        ) > 0
        then
        (
            SUM(t.transaction_amt) + 
            SUM(COALESCE(b.credit_balance,0)) +
            SUM(COALESCE(b.debit_balance,0))
        )
        end as debit_balance,
from 
    transaction t   

LEFT OUTER JOIN balance b ON (t.account_id = b.account_id 
                                        and t.transaction_date = b.transaction_date 
                                        and b.transaction_date=t.transaction_date- INTERVAL '1 DAYS')
group by
    t.account_id,
    t.transaction_date
请提供一些指针


编辑1:此查询未按预期方式运行。

一种方法是将您的逻辑分解为小查询,并最终将它们连接起来

select tw.account_id, tw.t_date,tw.t_c,th.T_D,fo.C_B,fi.d_B from 
(select account_id, Transaction_date as t_date, sum(Transaction_AMT) as t_C from TransactionTABLE 
where Transaction_AMT<0 group by  account_id, Transaction_date ) as tw inner join
(select account_id, Transaction_date as t_date, sum(Transaction_AMT) as t_d from TransactionTABLE 
where Transaction_AMT>0 group by  account_id, Transaction_date ) as th on tw.account_id=th.account_id and tw.t_date=th.t_date inner join
(select account_id, Transaction_date as t_date, sum(Transaction_AMT) as C_B from TransactionTABLE 
where sum(Transaction_AMT)<0 group by  account_id, Transaction_date ) as fo on th.account_id=fo.account_id and th.t_date=fo.t_date inner join
(select account_id, Transaction_date as t_date, sum(Transaction_AMT) as d_B from TransactionTABLE 
where sum(Transaction_AMT)>0 group by  account_id, Transaction_date ) as fi on fi.account_id=fo.account_id and fi.t_date=fo.t_date; 
否则

您可以尝试以下方法来计算交易日期和帐户id期间的d_B运行计数

select  account_id,
        transaction_date,
        SUM(transaction_amt) filter (where transaction_amt >= 0) as tot_debit,
        SUM(transaction_amt) filter (where transaction_amt < 0) as tot_credit,
sum(transaction_amt) over (partition by account_id where sum(transaction_amt)<0) as credit_balance,
sum(transaction_amt) over (partition by account_id where sum(transaction_amt)>=0) as debit_balance
from TransactionTABLE group by account_id, Transaction_date order by 1,2;

我已经编辑了我的问题并提供了工作查询,只是想知道哪种方式更好。也许,你可以通过测试两个查询的复杂性和执行时间来自己检查,然后你就有了答案!!我的查询未按预期方式运行。是否有任何方法可以减少查询中显示的连接以获得性能好处您可以在场景中使用over partition方法来计算此类运行计数。我不知道,我是否正确地使用了它们,你为什么不试一试呢?有关这方面的更多信息,请访问