MySQL Forums
Forum List  »  Newbie

Re: What is the SQL equvalance to VARIANCE
Posted by: Rick James
Date: November 03, 2010 04:07PM

There are other ways...

VARIANCE is STDDEV squared.
The two variances vary by a factor of COUNT/(count-1)
You don't have to compute the MEAN before starting the STDDEV; see the alternative formula used below, and in
http://en.wikipedia.org/wiki/Standard_deviation
SELECT
        city,
        COUNT(*),
        AVG(temp),
        ROUND(SUM(temp*temp)/COUNT(*) - AVG(temp)*AVG(temp), 2) AS Var1,
        ROUND(COUNT(*) / (COUNT(*)-1) *
            (SUM(temp*temp)/COUNT(*) - AVG(temp)*AVG(temp)), 2) AS Var_A,
        STD(temp)*STD(temp) AS 'STD**2',
        COUNT(*) / (COUNT(*)-1) *
            STD(temp)*STD(temp) AS 'STD_A**2'
    FROM temperatures
    GROUP BY DATE(date), city;
+-----------+----------+-----------+------+-------+--------+----------+
| city      | COUNT(*) | AVG(temp) | Var1 | Var_A | STD**2 | STD_A**2 |
+-----------+----------+-----------+------+-------+--------+----------+
| Paris     |        2 |   20.0000 | 4.00 |  8.00 | 4.0000 |   8.0000 |
| Stockholm |        2 |   10.0000 | 4.00 |  8.00 | 4.0000 |   8.0000 |
| Paris     |        2 |   20.0000 | 9.00 | 18.00 | 9.0000 |  18.0000 |
| Stockholm |        2 |   10.0000 | 9.00 | 18.00 | 9.0000 |  18.0000 |
| Paris     |        2 |   20.0000 | 1.00 |  2.00 | 1.0000 |   2.0000 |
| Stockholm |        2 |   10.0000 | 1.00 |  2.00 | 1.0000 |   2.0000 |
+-----------+----------+-----------+------+-------+--------+----------+

Options: ReplyQuote


Subject
Written By
Posted
Re: What is the SQL equvalance to VARIANCE
November 03, 2010 04:07PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.