First, my table:
mysql> desc invoice;
+-------------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| date | timestamp | YES | | NULL | |
| sent | timestamp | YES | | NULL | |
| due_date | timestamp | YES | | NULL | |
| amount | float | YES | | NULL | |
| amount_due | float | YES | | NULL | |
| status | enum('unpaid','paid') | YES | | NULL | |
| customer_id | int(11) | NO | MUL | NULL | |
+-------------+-----------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
Can anyone help me come up with a query that will return, for each row:
The day (so group by due_date) The starting balance of the day (This would be the end balance of the previous day) Inflow (the total of the POSITIVE transactions for the day) Outflow (the total of the NEGATIVE transactions for the day) Ending balance (The ending balance of the day after all the inflow and outflow transactions have been applied)
Basically every day I want to know how much money was deposited, how much money was withdrawn, the balance of the day started at, and the ending balance.
I should also specify that for the day, I want to group the rows by "due_date" and use the "amount" column as the basis for all transactions (ignoring the amount_due).
Any ideas?
I could start going down the path of making another table that holds just this info but thought maybe someone can come up with a nifty query that can do this off the actual data.