Trying to compute the median - Mailing list pgsql-general

From Maciej Bliziński
Subject Trying to compute the median
Date
Msg-id 20040511094555.GA19361@dobranet.polbox.pl
Whole thread Raw
Responses Re: Trying to compute the median
List pgsql-general
Hello,

I started to write the query that should compute the median.
Surprisingly, I get following error message:

"server closed the connection unexpectedly This probably means the server
terminated abnormally before or while processing the request."

I am using PostgreSQL 7.4.2, compiled from source under
Slackware-current, Linux 2.6.4.

Here's the query (it's not finished of course, but generates the error):

------------8<------------- ------------8<-------------

CREATE TEMPORARY TABLE test (
    value INTEGER PRIMARY KEY
);

INSERT INTO test VALUES (-1);
INSERT INTO test VALUES (0);
INSERT INTO test VALUES (3);
INSERT INTO test VALUES (5);
INSERT INTO test VALUES (8);

SELECT
    count(value) AS count,
    CASE
        WHEN mod(count(value), 2) = 1
        THEN
            /* odd number of elements */
            (
                SELECT value FROM test AS t2
                ORDER BY
                    value ASC
                OFFSET (count(t1.value) / 2)::INTEGER
                LIMIT 1
            )
        ELSE
            /* even number of elements */
            0.0
        END
        AS median
FROM
    test AS t1
;

------------8<------------- ------------8<-------------

Is it a PostgreSQL bug, or is my query so badly broken?

pgsql-general by date:

Previous
From: Rory Campbell-Lange
Date:
Subject: Postgres demo app [was: Re: nested elseif woes]
Next
From: Nick Barr
Date:
Subject: Re: Very slow query