我有以下表结构,我试图为其获取总计和小计并显示值的汇总。
ChartOfAccounts(AccountNumber, AccountDescription, ParentAccountNumber, IsControlAccount)
Ledger(LedgerId, JournalId, AccountNumber, IsDebit, Amount)
我已经设法使用 CTE 来获得所需的父子关系,但我不确定如何使用它来获取汇总到父账户中的控制账户余额。
到目前为止,我已经设法将以下查询放在一起,这并不完全是我想要的 --> SQL Fiddle。当前查询似乎没有正确汇总和分组父子总数。(我已经从小提琴中排除了年、月列)
描述问题的另一种方式是,所有控制帐户都应具有其子帐户的总数。
我需要的输出如下(年、月、AccountNumber、AccountDescription、DebitBalance、CreditBalance、Balance)
|Account#|Acc Desc | DR | CR | BAL |
|1000 |Accounts Receivable |10000 |5000 |5000 |
|1200 |Buyer Receivables |5000 |0 |5000 |
|12001 |Buyer Receivables - Best Buy |5000 |0 |5000 |
|1500 |Offers |5000 |5000 |0 |
|4000 |Accounts Payable | |4475.06 |4475.06 |
|4100 |Supplier Invoice Payables | |4475.06 |4475.06 |
|41002 |Supplier Invoice Payables - Knechtel | |4475.06 |4475.06 |
|6000 |Revenue | |524.93 |524.93 |
|6100 |Membership Fees Revenue | | |0 |
|6200 |Processing Fees Revenue | |100 |100 |
|62002 |Processing Fees Revenue - Knechtel | |100 |100 |
|6300 |Fees Revenue | |424.93 |424.93 |
|63002 |Fees Revenue - Knechtel | |424.93 |424.93 |