[Solved] How to calculate cumulative values in queries

Creating tables and queries
Post Reply
gkick
Posts: 324
Joined: Wed Aug 07, 2019 5:24 pm
Location: Chile

[Solved] How to calculate cumulative values in queries

Post by gkick »

Hi,

based on a transaction table I would like to display a chart showing cumulative income and expense
values. No idea how to go about it

the query in mind would look like the image below
For the chart I only would need the cum values for any given month end

Anyone having an example of cum s in sql ?

Thanks
Attachments
Screen Shot 05-26-21 at 08.17 PM.PNG
Screen Shot 05-26-21 at 08.17 PM.PNG (4.12 KiB) Viewed 3332 times
Last edited by gkick on Thu May 27, 2021 6:17 am, edited 2 times in total.
Libre Office 6.4.6 on Windows 10 HSQL 2.51 backend
FJCC
Moderator
Posts: 9575
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to calculate cumulative values in queries

Post by FJCC »

Here is one method.

Code: Select all

SELECT t1.id, t1.income, t1.expense, SUM(t2.income) AS Cum_Income, SUM(t2.expense) AS Cum_Exp
FROM Money t1 INNER JOIN Money t2 ON t1.id >= t2.id
GROUP BY t1.id, t1.income, t1.expense
query.JPG
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
gkick
Posts: 324
Joined: Wed Aug 07, 2019 5:24 pm
Location: Chile

Re: How to calculate cumulative values in queries

Post by gkick »

That's great, thank you so much!!!
Libre Office 6.4.6 on Windows 10 HSQL 2.51 backend
Post Reply