SELECT DAYOFYEAR(O.`date`) AS d, COUNT(*), (select count(*) from `orders` where DAYOFYEAR(`date`) <= d and `hasPaid` > 0) FROM `orders` as O WHERE O.`hasPaid` > 0 GROUP BY d ORDER BY d
This will require syntax tuning (I don't have MySQL to test it), but it shows you this idea. The subquery just has to go back and add everything fresh that you already included in the external query, and you need to do this for each row.
Take a look at this question on how to use connections to accomplish the same thing.
To solve performance degradation issues with data growth: since there is max. 366 days a year, and I assume that you have not fulfilled this request for several years, the subquery will be evaluated up to 366 times. With the correct hasPaid date and flag indexes, you'll be fine.
cdonner Mar 20 '09 at 1:47 2009-03-20 01:47
source share