我有这个查询
SELECT userId, orgQuery.timeUnit,
@SUM := @SUM + orgQuery.orderValue AS sum,
@COUNT := @COUNT + 1 AS count,
@AVG := @SUM / @COUNT AS avg
FROM (
SELECT userid, orderValue,
DATE_FORMAT(`acceptDate`, '%Y%M') AS timeUnit
FROM `agreements`
WHERE userId = 4
AND acceptDate > 2000-00-00
GROUP BY timeUnit
)
AS orgQuery,
(SELECT @COUNT := 0, @SUM := 0,@AVG :=0)
AS extra GROUP BY timeUnit
输出:
userId timeUnit sum count avg
4 201001 6000 1 6000.0000
4 201003 12000 2 6000.0000
4 201004 19500 3 6500.0000
但正如您所看到的,某些日期之间存在差距,我希望输出是一个连续的范围,例如:
userId timeUnit sum count avg
4 201001 6000 1 6000.0000
4 201002 0 2 3000.0000
4 201003 12000 3 6000.0000
4 201004 19500 4 4875.0000
此查询
(SELECT DATE_FORMAT(`acceptDate`, '%Y%M') AS timeUnit FROM `agreements` GROUP BY timeUnit )
输出完整的日期范围,但是当我尝试 LEFT JOIN 两个查询时,计数和平均值都搞砸了。我怎样才能得到我想要的结果?