I have the following table structures for which I am trying to get totals and subtotals and show a set of values.
ChartOfAccounts(AccountNumber, AccountDescription, ParentAccountNumber, IsControlAccount)
Ledger(LedgerId, JournalId, AccountNumber, IsDebit, Amount)
I managed to use CTE to get the necessary parent-child relationships, but I'm not sure how to use these control account balances, which are collapsed into parent accounts.
So far, I have managed to assemble the following query, which is not quite what I want -> SQL Fiddle . The current query does not seem to collapse and correctly groups the parent-child totals correctly. (I excluded the year, the columns of the month from the violin)
Another way to describe the problem is to say that all control accounts must have a total number of child accounts.
My required conclusion is the following (year, month, account number, account, debit balance, credit balance, 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 |
Ahmad source
share