X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 7499ED1B4ED for ; Thu, 16 Oct 2003 17:27:40 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 46275-06 for ; Thu, 16 Oct 2003 14:27:10 -0300 (ADT) Received: from smtp-1.indo.net.id (smtp-1.indo.net.id [202.159.32.91]) by svr1.postgresql.org (Postfix) with SMTP id C312DD1B508 for ; Thu, 16 Oct 2003 14:27:06 -0300 (ADT) Received: (qmail 30401 invoked from network); 17 Oct 2003 00:26:59 +0700 Received: from smtp.indo.net.id (202.159.32.81) by smtp-1.indo.net.id with SMTP; 17 Oct 2003 00:26:59 +0700 Received: (qmail 12089 invoked from network); 16 Oct 2003 17:24:34 -0000 Received: from unknown (HELO pop.makassar.indo.net.id) (202.159.23.19) by smtp.indo.net.id with SMTP; 16 Oct 2003 17:24:34 -0000 Received: FROM middinkcomp BY pop.makassar.indo.net.id ; Fri Oct 17 01:35:32 2003 +0700 Message-ID: <001101c3940c$29f004e0$1f00a8c0@middinkcomp> From: "Muhyiddin A.M Hayat" To: "Steve Crawford" Cc: References: <002e01c39356$d5885ca0$1f00a8c0@middinkcomp> <200310151331.22516.scrawford@pinpointresearch.com> Subject: Re: Calc Date: Fri, 17 Oct 2003 01:37:08 +0800 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/193 X-Sequence-Number: 15593 > Something like this: > > select id,db,cr,(select sum(cr-db) from calc sub where sub.id <= > calc.id) from calc; > > This of course assumes that ID indicates the correct order of the > entries and it will blow up if you allow nulls for cr or db (which > you shouldn't since that would literally be interpreted as "they > withdrew 10 and deposited an unknown amount"). If you have null > values already and they should be interpreted as 0 just do this: > > select id, db, cr, (select sum(coalesce(cr,0)-coalesce(db,0)) from > calc sub where sub.id <= calc.id) from calc; > > I assume no responsibility for potential lack of scalability of this > query. :) It's quite possible a faster solution exists - we'll see > what comes up on the list. > If data from "View" without ID, how can I do? My View: trx_date | trx_time | descriptions | payment_method | debet | credit | creator ------------+----------+--------------------------------------+------------- ---+-------+----------+--------- 2003-10-09 | 21:55:02 | Resto Biling : 13,800, Paid : 10,000 | Visa | 3800 | | middink 2003-10-16 | 03:28:30 | Payment - Thank You | Visa | | 40000.00 | middink 2003-10-08 | 18:17:40 | Payment - Thank You | Cash | | 50000.00 | middink