如何使用Oracle PL / SQL实现以下代码

时间:2019-06-28 11:31:30

标签: oracle plsql

我如何实现以下目标, 我必须从最近的帐单日期算起过去30天。使用以下公式进行会计处理

if ((sum of credits for 30 days from the latest statement date w.r. to account/
sum debits for 30 days from the latest statement date w.r. to account)
-sum debits for 30 days from the latest statement date w.r. to account)>0 then YES else NO

下面是来自两个不同表的两列,我需要根据这些列来推导每个帐户的公式。表A和表B中都存在帐户。

STATEMENT_DATE_LATEST; --from TableA
LATEST_BAL_IN_USD; -- from TableB

注意:上述声明中,“ /”被除以“-”是负号

1 个答案:

答案 0 :(得分:1)

如果我理解正确,这会有所帮助吗?

with temp as
  (select a.account,
          sum(case when b.transaction_amount >= 0 then b.transaction_amount end) sum_credits,
          sum(case when b.transaction_amount <  0 then b.transaction_amount end) sum_debits
   from table_a a join table_b b on a.account = b.account
   where b.statement_date > a.statement_date_latest + 30
   group by s.account
  )
select t.account,
       case when (t.sum_credits / t.sum_debits) - t.sum_debits > 0 then 'YES'
            else 'NO'
       end result
from temp t;