Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id EAFA5476943 for ; Mon, 6 Jan 2003 10:59:50 -0500 (EST) Received: from anchor-post-34.mail.demon.net (anchor-post-34.mail.demon.net [194.217.242.92]) by postgresql.org (Postfix) with ESMTP id 32BDB47696E for ; Mon, 6 Jan 2003 10:59:50 -0500 (EST) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-34.mail.demon.net with esmtp (Exim 3.35 #1) id 18VZfN-000FZk-0Y; Mon, 06 Jan 2003 15:59:53 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 57137175D9; Mon, 6 Jan 2003 15:59:52 +0000 (GMT) Received: from archonet.com (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with SMTP id ADB171758A; Mon, 6 Jan 2003 15:59:51 +0000 (GMT) Received: from 192.168.1.16 (SquirrelMail authenticated user dev) by mainbox.archonet.com with HTTP; Mon, 6 Jan 2003 15:59:51 -0000 (GMT) Message-ID: <1086.192.168.1.16.1041868791.squirrel@mainbox.archonet.com> In-Reply-To: <3E198E18.5090408@klaster.net> References: <3E197A39.8070808@klaster.net> <200301061341.32945.dev@archonet.com> <3E198E18.5090408@klaster.net> Date: Mon, 6 Jan 2003 15:59:51 -0000 (GMT) Subject: Re: order by and aggregate From: dev@archonet.com To: "Tomasz Myrta" Cc: pgsql-sql@postgresql.org X-Mailer: SquirrelMail (version 1.3.2) MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 X-Priority: 3 Importance: Normal X-Virus-Scanned: by AMaViS snapshot-20020531 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/38 X-Sequence-Number: 10903 > Richard Huxton wrote: > >> On Monday 06 Jan 2003 12:44 pm, Tomasz Myrta wrote: >> >> >Hi >> >I created my own aggregate function working as max(sum(value)) >> >It adds positive and negative values and finds maximum of this sum. >> >To work properly this function needs data to be sorted. >> >> >> I'm not sure that an aggregate function should require data to be sorted >> before processing. Could you show details of your function - there may >> be a >> way to rewrite it to handle unsorted data. > > Standard Postgresql aggregate functions don't need sorted data, but my > function needs. Look at the data: > > 3 3 > -2 1 > 6 7 *** max_sum=7 > -3 4 > 2 6 > > For example, if you inverse your data, you have: > > 2 2 > -3 -1 > 6 5 > -2 3 > 3 6 *** max_sum=6 > > As you see, data order is very important in this aggregate. Ah - so it's maximum of a running-total rather than a sum. AFAIK you are out of luck with aggregate functions. The order data is supplied to them is *not* defined - the "order by" operates just before results are output. Not much you can do about this, the whole basis of relational DBs are sets and sets don't have any idea of ordering. However, there are three options: You could define a cursor (or a table-function in 7.3) which would handle the order-by and then calculate the running-total on the fly. You then just need a standard max(running_total) call to pick out the value. Actually, if you use the cursor you might need to implement the max() in the application. Alternatively, you could add a running_total column and use a trigger to ensure the value is kept up to date. Finally, you could do the work in the application. Difficult to say which is the best for you. If you have 7.3, don't need these figures often and do a lot of updates/inserts I'd recommend option 1. If you're using 7.2, don't do a lot of inserts and want the figures frequently I'd choose option 2. HTH - Richard Huxton