Received: from sss2.sss.pgh.pa.us (sss.pgh.pa.us [209.114.166.2]) by hub.org (8.10.1/8.10.1) with ESMTP id e6ILhUh19038 for ; Tue, 18 Jul 2000 17:43:30 -0400 (EDT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss2.sss.pgh.pa.us (8.9.3/8.9.3) with ESMTP id RAA19150; Tue, 18 Jul 2000 17:43:04 -0400 (EDT) To: Jan Wieck cc: omid omoomi , BKermani@illumina.com, pgsql-sql@postgresql.org Subject: Re: Median In-reply-to: <200007181935.VAA12101@hot.jw.home> References: <200007181935.VAA12101@hot.jw.home> Comments: In-reply-to JanWieck@t-online.de (Jan Wieck) message dated "Tue, 18 Jul 2000 21:35:42 +0200" Date: Tue, 18 Jul 2000 17:43:03 -0400 Message-ID: <19147.963956583@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200007/177 JanWieck@t-online.de (Jan Wieck) writes: > I don't see any quick solution how to solve this problem with > an aggregate. Aggregates get all selected values in unsorted > order, and don't know ahead how many items there will be. > Even if, all this wouldn't be of any use, because you need to > look at the entire sorted list of selected items. Hmm. It would be a pretty straightforward extension of the existing support for DISTINCT aggregates to allow the agg function to receive all the inputs in sorted order along with a count of how many there are, whereupon a percentile aggregate would be trivial. Slow, but trivial. Probably a better way would be to skip evaluating the agg's transition function as such, and instead call the agg's final function just once with a pointer to the tuplesort object that contains the sorted input data. Then you reach in and pull out just the items you want, instead of having to read 'em all. Or you can scan 'em if you want. We might need to add a few features to the tuplesort API to allow access to the N'th item in the sorted data, but it's surely doable. Anyone care to work up a detailed proposal for something along this line? It seems like more work than it's worth to me, but if someone else wants to do the legwork... regards, tom lane