想要检索以下第一列的累计总和是我的查询:
SELECT COUNT(customer_id) AS entries, DATE(sold_date) as date,
(@cum_sum:=@cum_sum+COUNT(customer_id)) as cumulative
FROM sold_to
JOIN (select @cum_sum := 0) B
WHERE sold_date <= curdate()
GROUP BY DATE(sold_date)
LIMIT 0 , 30
但是,计算总和的结果与第一列相同:
答案 0 :(得分:0)
变量和group by
混合得不是很好。使用子查询:
SELECT date, entries,
(@cum_sum := @cum_sum + entries) as cumulative
FROM (SELECT date(sold_date) as date, count(*) as entries
FROM sold_to
WHERE sold_date <= curdate()
GROUP BY date(sold_date)
) s CROSS JOIN
(select @cum_sum := 0) params
ORDER BY date
LIMIT 0 , 30 ;