Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find a way to report the balance only after all postings of a transaction are applied #189

Open
dnicolodi opened this issue Jun 17, 2024 · 0 comments
Labels
feature New feature

Comments

@dnicolodi
Copy link
Collaborator

Given a ledger like

plugin "beancount.plugins.auto_accounts"

2024-06-16 * "Example"
  Assets:Foo                                        -1000.00 EUR
  Assets:Foo                                         
  Expenses:Bar                                          2.00 EUR

2024-06-17 * "Example"
  Assets:Foo                                           -1.00 EUR
  Expenses:Bar

trying to compute the maximum balance of the Assets:Foo account with a query like

SELECT date, balance WHERE account = 'Assets:Foo' ORDER BY balance ASC LIMIT 1

results in

   date       balance
──────────  ────────────
2024-06-17  -1000.00 EUR

which is likely not the desired result.

This happens because each posting is considered in isolation. This can be solved finding a way to report the balance only after all the postings relative to a transaction have been applied.

Making balance() a function similar to a windowed aggregate function, this could look something like:

SELECT 
  date, 
  balance(amount) AS balance
FROM (
  SELECT 
    date, 
    sum(position) AS amount
  FROM
    postings 
  GROUP BY 
    id
  )
ORDER BY
  balance 
LIMIT 1
@dnicolodi dnicolodi added the feature New feature label Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
None yet
Development

No branches or pull requests

1 participant