I read Create a Cumulative Sum Column in MySQL, and tried to adapt it to what I'm doing, but I can't seem to get it right.
The table:
Id (primary key)
AcctId
CommodId
Date
PnL
There is a unique index which contains AcctId, CommodId, Date. I want to get a cumulative total grouped by date.
This query
select c.date
, c.pnl
,(@cum := @cum + c.pnl) as "cum_pnl"
from commoddata c join (select @cum := 0) r
where
c.acctid = 2
and
c.date >= "2011-01-01"
and
c.date <= "2011-01-31"
order by c.date
will correctly calculate the running total for all records, showing data in the format
date pnl cum_pnl
======== ====== =======
2011-01-01 1 1
2011-01-01 1 2
2011-01-01 1 3
2011-01-01 1 4
2011-01-02 1 5
2011-01-02 1 6
...
(there can be many records per date). What I want is
date cum_pnl
======== =======
2011-01-01 4
2011-01-02 6
...
But nothing I've tried works. TIA.