需要汇总所有负面费用并创建新列并以正面费用复制

时间:2017-06-29 15:26:27

标签: sql oracle reporting accounting

我正在尝试创建一个财务报告,其中包含一个显示总计&#34;借记卡&#34; (积极或> 0金额)和&#34;积分&#34; (对于该特定帐号,这是负数或<0)。

目前,我正在提取帐号的所有交易,其中包含金额列中的正数和正数。我需要拆分它并将所有底片分成SUM并创建一个名为&#39; Credits&#39;并汇总所有积极因素并创建一个名为&#39; Debits&#39;。

的新列

我在这里看到另外一篇帖子,但该查询对我不起作用。

>     SELECT acc.account, acc.account_ty, coa.DESCR, dis.Amount, acc.create_dt, 
>     FROM GL_COA_ACCOUNT acc
>     JOIN GL_COA_CHART coa
>     ON acc.account = coa.account
>     JOIN AR_GL_DISTRIB dis
>     ON dis.GLCHART_SERNO = coa.GLCHART_SERNO
>     JOIN AR_RCT_LEDGER led
>     ON led.DISTRIB_SERNO = dis.distrib_serno
>     JOIN AR_RCT_INVHDR inv
>     ON led.invoice_num = inv.invoice_num


**Account account_ty   DESCR                               Amount create_dt DEBITS CREDITS NET AMOUNT**
XXX09   ASSET       XXX09-00-000-00  A/R Membership Dues   -55  09-AUG-16
XXX23   LIABILITY   XXX23-00-000-00  Checks / Cash Clearing 55  07-SEP-16
XXX09   ASSET       XXX09-00-000-00  A/R Membership Dues    55  09-AUG-16
XXX02   INCOME      XXX02-01-000-00  Predoctoral Student D -55  07-SEP-16
XXX09   ASSET       XXX09-00-000-00  A/R Membership Dues   -55  09-AUG-16
XXX23   LIABILITY   XXX23-00-000-00  Checks / Cash Clearing 55  07-SEP-16
XXX09   ASSET       XXX09-00-000-00  A/R Membership Dues    55  09-AUG-16

3 个答案:

答案 0 :(得分:2)

您需要的是条件聚合

select ......,   sum(case when amount < 0 then amount end) as credits,
                 sum(case when amount > 0 then amount end) as debits
from   ......
group by account_number

答案 1 :(得分:0)

我会建议这样的事情:

SELECT account,
       sum((amount>0)*amount) as debit, 
       sum((amount<0)*amount) as credit
FROM table
GROUP BY account

答案 2 :(得分:0)

创建一个检查金额符号(和NULLS)并指定非空值和总和的表达式

我只是确保我的表达式是非NULL(所有记录都有非NULL数字赋值),然后我求和。

作为Oracle SQL开发人员,我偏向于decodesign等Oracle功能。

     SCOTT@dev>WITH test_data AS (
  2      SELECT
  3          4.32 amount, '100' account
  4      FROM
  5          dual
  6      UNION ALL
  7      SELECT 3.23, '100'
  8      FROM
  9          dual
 10      UNION ALL
 11      SELECT
 12          -3.22, '100'
 13      FROM
 14          dual
 15      UNION ALL
 16      SELECT
 17          -4.22, '100'
 18      FROM
 19          dual
 20      UNION ALL
 21      SELECT
 22          4.22, '200'
 23      FROM
 24          dual
 25      UNION ALL
 26      SELECT
 27          0, '200'
 28      FROM
 29          dual
 30      UNION ALL
 31      SELECT
 32          NULL, '200'
 33      FROM
 34          dual
 35  ) SELECT
 36      account,
 37      SUM(DECODE( sign(nvl( amount, 0) ), 1, amount, 0) ) debits,
 38      SUM(DECODE( sign(nvl( amount, 0) ), -1, amount, 0) ) credits
 39  FROM
 40      test_data
 41  GROUP BY
 42      account;
ACCOUNT  DEBITS  CREDITS  
100      7.55    -7.44    
200      4.22    0