我想计算数据的累计平均值。
在我的查询中,我现在获得每个日期的总和值。但是我很困惑如何计算累计平均值。
我的查询
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))
在这里,我得到了
sum(m.protein)
的总和值,但是我想在这里累积平均值 最佳答案
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))
关于mysql - 如何计算累计平均值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24858748/