I've got the following query
SELECT date, t1.col1, t2.col2, t1.col1 * t2.col2 AS `col3`
FROM t1
INNER JOIN t2 ON t2.col3 = t1.col3
WHERE t2.col3 = 2
ORDER BY date ASC
Now I want to add a column that is a cumulative sum of col3
, so following this question (Create a Cumulative Sum Column in MySQL) I came up with this:
SET @csum := 0;
SELECT date, t1.col1, t2.col2, t1.col1 * t2.col2 AS `col3`, (@csum := @csum + `col3`) AS `sum`
FROM t1
INNER JOIN t2 ON t2.col3 = t1.col3
WHERE t2.col3 = 2
ORDER BY date ASC
But as col3
is a generated in the query as well, it obviously doesn't work. How can I change it so the generated column can have a cumulative sum?