X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id E8E5C474E5C for ; Sat, 1 Mar 2003 12:54:14 -0500 (EST) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with SMTP id 38136-04 for ; Sat, 1 Mar 2003 12:53:42 -0500 (EST) Received: from sabre.velocet.net (sabre.velocet.net [216.138.209.205]) by postgresql.org (Postfix) with ESMTP id 8575E474E42 for ; Sat, 1 Mar 2003 12:53:42 -0500 (EST) Received: from stark.dyndns.tv (H162.C233.tor.velocet.net [216.138.233.162]) by sabre.velocet.net (Postfix) with ESMTP id 83F3D138058; Sat, 1 Mar 2003 12:53:42 -0500 (EST) Received: from localhost ([127.0.0.1] helo=stark.dyndns.tv ident=foobar) by stark.dyndns.tv with smtp (Exim 3.36 #1 (Debian)) id 18pBB8-0006hB-00; Sat, 01 Mar 2003 12:53:42 -0500 To: Rod Taylor Cc: Edmund Lian , pgsql-sql@postgresql.org Subject: Re: Denormalizing during select References: <200302241433.49502.josh@agliodbs.com> <1046538423.26763.177.camel@jester> In-Reply-To: <1046538423.26763.177.camel@jester> From: Greg Stark Organization: The Emacs Conspiracy; member since 1992 Date: 01 Mar 2003 12:53:41 -0500 Message-ID: <87d6lbkm6y.fsf@stark.dyndns.tv> Lines: 40 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: by amavisd-new X-Archive-Number: 200303/15 X-Sequence-Number: 12055 Rod Taylor writes: > I've been trying to figure out how to give a running total (similar > issue I think). Running totals are a "hard problem". They certainly cannot be solved using aggregates. They're similar to the ranking problem of assigning a sequential number to each item within each group. The problem is they share certain properties of aggregate functions, namely that they require persistent state storage and a state transition function. But they definitely aren't aggregate functions in that they return a value for every record, not one for the whole group. I'm not even clear how to write an embedded (plpgsql or perl or python) function, since I'm not clear how to allocate space for the state that will be available for each call on each record but independent from other calls in the same query. You have to be able to handle two running totals at the same time. Note that running totals are not very sql-ish. Since sql deals in unordered sets a running total is pretty ill-defined. It would have to be calculated after the sort operation or else require you to sort the input tables in a subquery or something. To write a proper well-defined sql-ish query for running totals you would have to do the very inefficient: select employee_id, salary, (select count(*) from employees x where x.salary < employee.salary) as salary_rank, (select sum(salary) from employees x where x.salary < employee.salary) as running_total from employees order by salary desc Finding a way to transform that into the single-scan plan that's obviously the right way to execute it would be really cool but seems pretty far-fetched. I don't think any database is capable of it today. -- greg