X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 322755E46F4 for ; Sat, 28 Aug 2004 13:19:58 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 15362-04 for ; Sat, 28 Aug 2004 16:19:57 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 2833B5E46D2 for ; Sat, 28 Aug 2004 13:19:48 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i7SGJbDO020038; Sat, 28 Aug 2004 12:19:37 -0400 (EDT) To: Greg Stark Cc: Josh Berkus , Scott Gerhardt , pgsql-sql@postgresql.org Subject: Re: Aggregate query for multiple records In-reply-to: <87eklrlkwh.fsf@stark.xeocode.com> References: <641FEEDD-F728-11D8-A763-000393801C60@g-it.ca> <200408270948.00680.josh@agliodbs.com> <200408271159.26445.josh@agliodbs.com> <87eklrlkwh.fsf@stark.xeocode.com> Comments: In-reply-to Greg Stark message dated "28 Aug 2004 04:35:58 -0400" Date: Sat, 28 Aug 2004 12:19:37 -0400 Message-ID: <20037.1093709977@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200408/316 X-Sequence-Number: 18813 Greg Stark writes: > [ nice example snipped ] > ... Also, you'll have to change it to use reals. That part, at least, can be worked around as of 7.4: use polymorphic functions. You can declare the functions and aggregate as working on anyelement/anyarray, and then they will automatically work on any datatype that has a + operator. regression=# create or replace function first_6_accum (anyarray,anyelement) returns anyarray regression-# language sql immutable as 'select case when array_upper($1,1)>=6 then $1 else $1||$2 end'; CREATE FUNCTION regression=# create function sum_6(anyarray) returns anyelement immutable language sql as 'select $1[1]+$1[2]+$1[3]+$1[4]+$1[5]+$1[6]'; CREATE FUNCTION regression=# create aggregate sum_first_6 (basetype=anyelement, sfunc=first_6_accum, stype=anyarray,initcond='{}',finalfunc=sum_6); CREATE AGGREGATE regression=# select sum_first_6(i) from (select i from (select 1 as i union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8) as x order by i desc) as x; sum_first_6 ------------- 33 (1 row) regression=# select sum_first_6(i) from (select i from (select 1.1 as i union select 2 union select 3 union select 4 union select 5 union select 6 union select 7.7 union select 8) as x order by i desc) as x; sum_first_6 ------------- 33.7 (1 row) regression=# regards, tom lane