(I am using PostgreSQL)
I have a table which stores transactions to an account. For the sake of this question imagine it is structured as follows:
create table transactions (
timestamp timestamp with time zone not null,
amount decimal(6,2) not null
);
The balance at any point in time can be obtained by:
select sum(amount) as balance from transactions where timestamp < 'point in time';
How can I find all periods of time where the balance (the sum of amounts up to that point in time) was equal to a certain value (the most interesting value for me being 0, but a general solution would be nice)?
I am aware of the solution that involves denormalising balance into its own column. I would prefer not to do that for other reasons, so I am looking for suggestions using the schema above.
Try this:
Find all zero: