在n个周期内移动/滚动标准偏差

Trying to get a rolling standard deviation in MYSQL over a period of time. I have tried the following code, but it doesn't reconcile when I do a check in Excel.

Here is my attempt:

SELECT STDDEV_POP(sales) FROM (SELECT `sales` FROM `table` WHERE `company` = '". $company ."' AND `date` <= '". $date ."' ORDER BY `date` DESC LIMIT $period) AS subquery

I've used $date so I can limit the data if I need it before a certain date. $period specifies the time the standard deviation is calculated over (Eg. 5 days).

EDIT:

As requested, just some further details. The attached image are the checks done in Excel, I've just taken the data from here.

Excel Worksheet

The SQL Fiddle is here. The value reconciles back to the Excel sheet when the date is '2012-12-15', but doesn't at '2012-12-16' or '2016-12-17'

Got figures generated by MySQL and Excel to reconcile.

1) The method used in the link at stockcharts calculates standard deviation off the 10 day average of the deviations squared. THis isn't the normal approach to calculating standard deviation.

2) When testing in Excel, I used the STDEV() function, which assumes a sample, however, the function used in MySQL was STDDEV_POP() which assumes a population.

These two points are why the numbers wouldn't reconcile. All good now!