Re: stddev returns 0 when there is one row - Mailing list pgsql-general

From Tom Lane
Subject Re: stddev returns 0 when there is one row
Date
Msg-id 23308.1050784406@sss.pgh.pa.us
Whole thread Raw
In response to Re: stddev returns 0 when there is one row  (Joe Conway <mail@joeconway.com>)
Responses Re: stddev returns 0 when there is one row  (Joe Conway <mail@joeconway.com>)
List pgsql-general
Joe Conway <mail@joeconway.com> writes:
> So I'd take it that PostgreSQL's STDDEV implements STDDEV_POP.

No, we implement the sample standard deviation, as stated in the docs:
http://www.ca.postgresql.org/users-lounge/docs/7.3/postgres/functions-aggregate.html

The code is pretty straightforward, at least in the float8 case:

    /* We define STDDEV of no values to be NULL, of 1 value to be 0 */
    if (N == 0.0)
        PG_RETURN_NULL();

    if (N <= 1.0)
        PG_RETURN_FLOAT8(0.0);

    numerator = N * sumX2 - sumX * sumX;

    /* Watch out for roundoff error producing a negative numerator */
    if (numerator <= 0.0)
        PG_RETURN_FLOAT8(0.0);

    PG_RETURN_FLOAT8(sqrt(numerator / (N * (N - 1.0))));

I don't have a real strong feeling about whether we should change the
behavior at N=1 or not.  Does the SQL200x spec provide any guidance?

            regards, tom lane


pgsql-general by date:

Previous
From: elein
Date:
Subject: Re: Please some help on a join question with sum aggregate
Next
From: Bruno Wolff III
Date:
Subject: Re: stddev returns 0 when there is one row