Skip to content
Advertisement

SQL Query to group opening balance value

I am relatively new to SQL queries, and Stack Overflow has previously pointed me in the right direction, either by reading other peoples issues or answering my specific question. But this one I can not find anything that would resolve my issue.

I have two data tables which I have joined together to create a list of transactions which belong to a particular account. This part I have done and works (probably not the most efficient coding, but it is to the best of my knowledge). I can post the code if required. The results look like this, the Balance column is calculated in the SQL query and is not in any table: enter image description here

This is great but I want to include an Opening Balance line at the start which would sum all records in this account before a specific date and the balance would include the opening amount. I would like it to look like this: enter image description here

Adjusted Code I am using:

This all seems to work fine, where I had to union the 2 tables for the opening balance and the transactions, so lots of unions there.

This is what the results look like:

enter image description here

Just one point, on the opening balance line, I would like the Amount to be blank, but if I leave it out of the opening balance query, the balance carried forward is wrong. It is on line 17 of the code. Not sure it can be done.

Once again any advise would be much appreciated.

Advertisement

Answer

Without having all your tables and going through a full setup and sample populating, I can offer the following…

Your INNER query is pre-grabbing all the transactions and details and union together into one set… of which you then outer query sum them.

I would add one more adjust the query something like (and again, just guidance vs full data prep and all).

and change to

In this case, you can always run the new query for balance forward on its own merit to make sure that makes syntax and aggregation sense you INTEND to get. By using the WHERE clause to limit the top queries to only include those AFTER whatever cut-off date, those will be their own. This new query will pre-aggregate into a single row (per ledger) for all transactions PRIOR TO the cutoff leaving only 1 record (per ledger). Using the MAX() on the transaction date will make sure it floats to the top by date order and should be first thus starting your balance forward value.

Hope this make sense as the rest of the query you have is already working. Here, you are in essence just pre-querying all records ON or PRIOR TO the cutoff date and adding to the list. So build this query on its own first, THEN adjust your existing to include as additional union.

For your final removal of opening balance amount. You have a controlled value of the ID = -1. Use a case/when to remove. In your OUTERMOST query, change

Revision to clear opening balance amount line…

to

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement