I could use some help (preferably a dummy's guide) to updating the following table:
CREATE TABLE `SYMBOL` (
`day` date NOT NULL,
`open` decimal(8,3) DEFAULT NULL,
`high` decimal(8,3) DEFAULT NULL,
`low` decimal(8,3) DEFAULT NULL,
`close` decimal(8,3) DEFAULT NULL,
`volume` bigint(20) DEFAULT NULL,
`adj_close` decimal(8,3) DEFAULT NULL,
`moving_average` decimal(8,3) DEFAULT NULL,
PRIMARY KEY (`day`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The moving_average column is empty now. All other columns are populated (for the time being, I'm ok with this being "static", it does not need to update as I add rows - though if this is easy to do, that would be great). It is a 20-day moving average that I hope to calculate.
I have tried by performing the steps here to the best of my ability:
How do I calculate a moving average using MySQL?
My query is this:
SELECT
`close`,
(
SELECT
AVG(`close`) AS moving_average
FROM
SYMBOL T2
WHERE
(
SELECT
COUNT(*)
FROM
SYMBOL T3
WHERE
`day` BETWEEN T2.day AND T1.day
) BETWEEN 1 AND 20
)
FROM
SYMBOL T1
Have I modified the query correctly? What needs to be done to write the results to the moving_average column?
When I run the above, nothing happens (it says its running, no errors, after letting it run for a long time I just stopped it). The column moving_average still has NULL values.
I also looked at this answer: How to calculated multiple moving average in MySQL
However, I'm unsure what I need to change to the reply for my table.
Any help is appreciated.