I want to calculate cumulative average of my data.
in my query i get sum value for every date now.But I am confused How to calculate calculate cumulative average.
my query
SELECT
u.id,
(SUM(m.protein)),
DATE(FROM_UNIXTIME(u.create)) AS Cdate,
goal.what,
goal.value
FROM
`meal` AS m,
`user_history` AS u
LEFT JOIN goal
ON goal.what = 'Protein'
AND goal.user_id = 48
WHERE u.meal_id = m.id
AND u.user_id = 48
GROUP BY DATE(FROM_UNIXTIME(u.create))
Here I get the sum value by sum(m.protein)
but i want here cumulative average
SET @csum=0;
SELECT
u.id,
(SUM(m.protein)) as sum1,
(@csum := @csum + sum1) as CSUM,
DATE(FROM_UNIXTIME(u.create)) AS Cdate,
goal.what,
goal.value
FROM
`meal` AS m,
`user_history` AS u
LEFT JOIN goal
ON goal.what = 'Protein'
AND goal.user_id = 48
WHERE u.meal_id = m.id
AND u.user_id = 48
GROUP BY DATE(FROM_UNIXTIME(u.create))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.