Beancount Query Language - SQL-like Financial Queries
Beancount features a powerful, SQL-like Query Language (BQL) that allows you to slice, dice, and analyze your financial data with precision. Whether you want to generate a quick report, debug an entry, or perform complex analysis, mastering BQL is key to unlocking the full potential of your plaintext accounting ledger. This guide will walk you through its structure, functions, and best practices. 🔍
Query Structure and Execution
The core of BQL is its familiar, SQL-inspired syntax. Queries are executed using the bean-query
command-line tool, which processes your ledger file and returns the results directly in your terminal.
Basic Query Format
A BQL query is composed of three main clauses: SELECT, FROM, and WHERE.
SELECT <target1>, <target2>, ...
FROM <entry-filter-expression>
WHERE <posting-filter-expression>;
SELECT
: Specifies which columns of data you want to retrieve.FROM
: Filters entire transactions before they are processed.WHERE
: Filters the individual posting lines after the transaction has been selected.
Two-Level Filtering System
Understanding the difference between the FROM
and WHERE
clauses is crucial for writing accurate queries. BQL uses a two-level filtering process.
-
Transaction Level (
FROM
) This clause acts on entire transactions. If a transaction matches theFROM
condition, the entire transaction (including all its postings) is passed to the next stage. This is the primary way to filter data, as it preserves the integrity of the double-entry accounting system. For example, filteringFROM year = 2024
selects all transactions that occurred in 2024. -
Posting Level (
WHERE
) This clause filters the individual postings within the transactions selected by theFROM
clause. This is useful for presentation and for focusing on specific legs of a transaction. However, be aware that filtering at this level can "break" the integrity of a transaction in the output, as you might only see one side of an entry. For example, you could select all postings to yourExpenses:Groceries
account.