【问题标题】:how can I do it我该怎么做
【发布时间】:2018-01-16 05:10:25
【问题描述】:

`

SELECT TXNID,
       TOTAL_AMOUNT,
       ACCOUNT_CATEGORY,
       DEBIT 
FROM ACCOUNTING_BOOK 
  WHERE TXNID like 'M003%' ;

TXNID:M003 TOTAL_AMT:25 ACCOUNT_CATEGORY:Revenue Debit:null Credit:null

TXNID:M003 TOTAL_AMT:25 ACCOUNT_CATEGORY:Asset Debit:null Credit:null

借方栏填写规则:[Total-Amount if account-category=Expense or if account-category=Asset for对应的TxnId匹配的Revenue项目;否则为 0]

想要将此表内的结果验证到借方列。我该怎么做?

信用栏填写规则:[Total-Amount if account-category=Revenue or if account-category=Asset for对应的Expense item对应TxnId将返回1; 0否则] 想把这个表里面的结果验证到credit栏里,怎么办?

【问题讨论】:

  • 添加您的示例数据和预期输出,以便人们理解
  • 现在可以出发了吗?

标签: oracle plsql


【解决方案1】:

如果我理解正确的规则:

update accounting_book ab
  set debit = case when account_category in ('Expense', 'Asset')
                   then (select sum(total_amt)
                           from accounting_book 
                           where account_category = 'Revenue'
                             and txnid = ab.txnid)
                   else 0
              end

编辑:

填充信用栏的规则:[Total-Amount if account-category=收入或者如果 account-category=Asset for 与 TxnId 匹配的对应 Expense 项将返回 1 ; 0 否则]

与之前的更新类似:

update accounting_book ab
  set credit = case when account_category in ('Revenue', 'Asset')
                     and exists (select 1  
                                   from accounting_book 
                                   where account_category = 'Expense'
                                     and txnid = ab.txnid)
                    then 1
                    else 0
               end

【讨论】:

  • @saurav - 如果在 SQL 中完成,你为什么想要一个函数?也许您需要再次编辑您的问题,详细了解您的情况。
  • 存储可以计数的值并不是一个好习惯,因为它们可能会变得无效。我宁愿使用视图。
  • 我只是试试这个,但在某些领域它返回空值。这是什么原因?先生能解释一下吗?
  • 可能是因为没有对应的Revenue行?而sum(null)null。你看到你的数据,请检查。 ;-)
  • 贷记栏填写规则:[Total-Amount if account-category=Revenue or if account-category=Asset for对应的Expense item对应TxnId将返回1; 0 否则] 想要将此表内的结果验证到信用列中。我该怎么做?
【解决方案2】:
     CASE WHEN A.ACCOUNTCATEGORY IN('Revenue','Asset')
              AND EXISTS (SELECT TOTAL_AMOUNT
              FROM ACCOUNTING_BOOK
              WHERE ACCOUNTCATEGORY='Expense'
              AND TXNID=A.TXNID)
              THEN A.TOTAL_AMOUNT
              ELSE 0
              END, 

               CASE WHEN A.ACCOUNTCATEGORY IN('Expense','Asset')
              AND EXISTS (SELECT TOTAL_AMOUNT
              FROM ACCOUNTING_BOOK
              WHERE ACCOUNTCATEGORY='Revenue'
              AND TXNID=A.TXNID)
              THEN A.TOTAL_AMOUNT
              ELSE 0
              end);


 END LOOP;
 END;
 /

【讨论】:

  • 当我这样做时,借方列给了我正确的输出,但贷方列为 0,因为它包含负值。我想在光标的帮助下使用贷方列而不使用更新语句。
  • 贷记栏填写规则:[Total-Amount if account-category=Revenue or if account-category=Asset for对应Expense item by TxnId ; 0 否则]
  • 请帮帮我。
猜你喜欢
  • 2012-05-26
  • 1970-01-01
  • 2014-01-23
  • 2011-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多