Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (8.11.3/8.11.4) with ESMTP id f9CM8kr55511 for ; Fri, 12 Oct 2001 18:08:46 -0400 (EDT) (envelope-from josh@agliodbs.com) Received: from [209.10.40.250] (account ) by davinci.ethosmedia.com (CommuniGate Pro WebUser 3.4.1) with HTTP id 147007; Fri, 12 Oct 2001 15:08:42 -0700 From: "Josh Berkus" Subject: Re: MEDIAN as custom aggregate? To: Allan Engelhardt Cc: pgsql-sql@postgresql.org X-Mailer: CommuniGate Pro Web Mailer v.3.4.1 Date: Fri, 12 Oct 2001 15:08:42 -0700 Message-ID: In-Reply-To: <3BC761D0.92BB9D90@cybaea.com> MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Archive-Number: 200110/159 X-Sequence-Number: 5114 Allan, > Can't you do something like > > select age from ages order by age limit 1 offset (select count(*) > from ages)/2; > > except you can't nest the select so you'll have to use a variable to > hold it... > > Make sure it does the right thing when there is an odd number of > rows. Duuuuuuuh! I feel like a real idiot now. The query I'll use is this: SELECT site, COUNT(pageviews) as count_views, AVG(pageviews) AS mean_views, median_views FROM sites, (SELECT pageviews as median_view FROM pageviews LIMIT 1 OFFSET middlerec('pageviews')) med GROUP BY site, median_views; Where middlerec is a custom function that counts the records and returns the middle one. -Josh ______AGLIO DATABASE SOLUTIONS___________________________ Josh Berkus Complete information technology josh@agliodbs.com and data management solutions (415) 565-7293 for law firms, small businesses fax 621-2533 and non-profit organizations. San Francisco