Trying to compute the median

From: Maciej Bliziński <maciej(dot)blizinski(at)dobranet(dot)polbox(dot)pl>
To: pgsql-general(at)postgresql(dot)org
Subject: Trying to compute the median
Date: 2004-05-11 09:45:55
Message-ID: 20040511094555.GA19361@dobranet.polbox.pl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Nick Barr 2004-05-11 09:50:34 Re: Very slow query
Previous Message Rory Campbell-Lange 2004-05-11 09:13:29 Postgres demo app [was: Re: nested elseif woes]