I have a table with values like the following
Name DatePurchased QuantityPurchased
A 2/3/2012 1
A 2/4/2012 1
A 2/5/2012 2
B 2/2/2012 1
B 2/3/2012 2
I want to output the following
Name DatePurchased QuantityPurchased
A 2/3/2012 1
A 2/4/2012 2 // as I have purchased 2 upto this date
A 2/5/2012 4 // as I have purchased 4 upto this date
B 2/2/2012 1
B 2/3/2012 3
My query
SELECT Name, `DatePurchased` , SUM(QuantityPurchased)
FROM table1
GROUP BY DatePurchased
does not do the math right. I know whats wrong but can’t figure out the solution.
Thanks
Try:
This joins
table1to itself within Name and such thatt1s date is always at leastt2s date, and sums upt2sQuantityPurchased(for each name,date int1).(Try performing the same query with
SELECT *, and without theSUMandGROUP BYto see the joined table. Then theSUMandGROUP BYwill become clear).