I have a MySQL table with 4 columns as follows.
TransactionID | Item | Amount | Date
------------------------------------
| | |
| | |
I have a lot of entries and what I want to be able to do is create a query that returns a list of the total Profit/Loss at each point in time?
Imagine I had the following transactions:
Bought item for 5
Sold item for 15
Bought item for 5
Sold item for 15
So I would want it to return something like this.
Profit/Loss | Date
------------------
-5 | 20-10-12
10 | 21-10-12
5 | 22-10-12
20 | 23-10-12
Is this possible with a MySQL query?
Assuming that
Dateis stored as you show on the expected result this should work:Otherwise id
Dateis of typeDATE,DATETIMEorTIMESTAMPyou could do something like this:references:
EDIT (after OP’s comment)
to achieve the comulative SUM here is a good hint:
essentialy you store the current sum into a variable (@csum) and for each row of the grouped transactions you increase it by the daily balance