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 87D6B52887 for ; Tue, 31 May 2005 14:18:19 -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 67313-02 for ; Tue, 31 May 2005 17:18:09 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.197.194]) by svr1.postgresql.org (Postfix) with SMTP id F3D6B52891 for ; Tue, 31 May 2005 14:18:08 -0300 (ADT) Received: (qmail 8350 invoked by uid 500); 31 May 2005 17:17:08 -0000 Date: Tue, 31 May 2005 12:17:08 -0500 From: Bruno Wolff III To: lucas@presserv.org Cc: pgsql-sql@postgresql.org Subject: Re: Sum() rows Message-ID: <20050531171708.GB5899@wolff.to> Mail-Followup-To: Bruno Wolff III , lucas@presserv.org, pgsql-sql@postgresql.org References: <20050531133648.cypdc5lnjtc0wsws@www.presserv.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050531133648.cypdc5lnjtc0wsws@www.presserv.org> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.008 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200505/243 X-Sequence-Number: 21556 On Tue, May 31, 2005 at 13:36:48 -0300, lucas@presserv.org wrote: > Hi. > How can I sum a row and show the sum for each row??? Since in your example the id field gives the ordering, you can use a subselect to add up the subtotal for rows with and id less than or equal to the value of id for the current row. > For example, in a finances table that have the total movimentation(debit/credit) > in the bank. > > i.e: > CREATE TABLE TB1 (id integer primary key, value numeric); > insert into tb1 values (1,20); > insert into tb1 values (2,2); > insert into tb1 values (3,3); > insert into tb1 values (4,17); > insert into tb1 values (5,-0.5); > insert into tb1 values (6,3); > > I want a query that returns: > -id- | --- value --- | --- subtot --- > 1 | 20.00 | 20.00 > 2 | 2.00 | 22.00 > 3 | 3.00 | 25.00 > 4 | 17.00 | 42.00 > 5 | -0.50 | 41.50 > 6 | 3.00 | 44.50 > > The subtot colum will be the "prev. subtot colum"+"value colum". :-/ > I dont know how to make the "subtot" colum, I tried to use the sum() function > but it not works correctly. > Any idea??? > > Thanks. > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faq