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 A979053374 for ; Wed, 20 Apr 2005 01:39:44 -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 14281-10 for ; Wed, 20 Apr 2005 04:39:39 +0000 (GMT) Received: from ausimss.pervasive.com (ausimss.pervasive.com [66.45.103.246]) by svr1.postgresql.org (Postfix) with ESMTP id 003EB5333C for ; Wed, 20 Apr 2005 01:39:35 -0300 (ADT) Received: from ausbayes1.aus.pervasive.com ([172.16.8.6]) by ausimss.pervasive.com with InterScan Messaging Security Suite; Tue, 19 Apr 2005 23:39:35 -0500 Received: from ausmail2k3.aus.pervasive.com ([172.16.4.8]) by ausbayes1.aus.pervasive.com with Microsoft SMTPSVC(5.0.2195.6713); Tue, 19 Apr 2005 23:39:34 -0500 Received: from BLRMAIL1.aus.pervasive.com ([10.3.16.60]) by ausmail2k3.aus.pervasive.com with Microsoft SMTPSVC(5.0.2195.6713); Tue, 19 Apr 2005 23:39:32 -0500 X-MIMEOLE: Produced By Microsoft Exchange V6.0.6249.0 content-class: urn:content-classes:message Subject: Re: Debet-Credit-Balance Calculation MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01C54562.EDAA9C97" Date: Wed, 20 Apr 2005 10:09:24 +0530 Message-ID: <02767D4600E59A4487233B23AEF5C5992A407A@blrmail1.aus.pervasive.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [SQL] Debet-Credit-Balance Calculation Thread-Index: AcVFAaKRxkKEpdz5RlKXFzxiN31CugAYKLSw From: "Ramakrishnan Muralidharan" To: "Muhyiddin A.M Hayat" , X-OriginalArrivalTime: 20 Apr 2005 04:39:32.0964 (UTC) FILETIME=[F2A07240:01C54562] X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.246 tagged_above=0 required=5 tests=AWL, HTML_60_70, HTML_MESSAGE X-Spam-Level: X-Archive-Number: 200504/346 X-Sequence-Number: 21239 This is a multi-part message in MIME format. ------_=_NextPart_001_01C54562.EDAA9C97 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable =20 Hi,=20 Using a table with larger data in the sub-query always eats up CPU = time and it is time consuming. The below given statement uses the = transaction table two times in the sub-query and for processing every = record, it have to go through the same table twice in the sub-query. It = is better to write a function to process the data and return back the = record set. =20 Regards, R.Muralidharan =20 -----Original Message----- From: Muhyiddin A.M Hayat [mailto:middink@indo.net.id] Sent: Tuesday, April 19, 2005 10:18 PM To: pgsql-sql@postgresql.org Subject: [SQL] Debet-Credit-Balance Calculation Dear All, I have problem to calculation balance from debet and credit. my transaction table: =20 id | trx_timestamptz | account | trx_type_id | amount ----+------------------------+------------------+-------------+----------= --- 3 | 2005-04-14 17:16:49+08 | 01.2010100.00002 | 1 | = 1000000.00 4 | 2005-04-14 17:17:12+08 | 01.2010100.00002 | 1 | = 50000.00 5 | 2005-04-14 17:17:12+08 | 01.2010100.00003 | 1 | = 600000.00 6 | 2005-04-14 17:17:47+08 | 01.2010100.00002 | 2 | = 7000.00 7 | 2005-04-16 00:32:50+08 | 01.2010100.00003 | 1 | = 20000.00 11 | 2005-04-16 02:45:06+08 | 01.2010100.00002 | 1 | = 100000.00 12 | 2005-04-16 02:46:02+08 | 01.2010100.00002 | 1 | = 20000.00 13 | 2005-04-16 02:46:59+08 | 01.2010100.00002 | 2 | = 163000.00 14 | 2005-04-16 02:50:17+08 | 01.2010100.00005 | 1 | = 100000.00 15 | 2005-04-16 02:53:42+08 | 01.2010301.00001 | 1 | = 100000.00 16 | 2005-04-16 02:57:22+08 | 01.2010100.00001 | 1 | = 2000000.00 17 | 2005-04-16 23:56:44+08 | 01.2010200.00000 | 1 | = 10000000.00 18 | 2005-04-17 18:58:57+08 | 01.2010100.00003 | 1 | = 100000.00 19 | 2005-04-17 19:13:05+08 | 01.2010100.00002 | 1 | = 100000.00 20 | 2005-04-17 19:13:45+08 | 01.2010100.00002 | 1 | = 200000.00 21 | 2005-04-17 19:15:36+08 | 01.2010100.00002 | 1 | = 50000.00 22 | 2005-04-17 19:17:17+08 | 01.2010100.00005 | 2 | = 10000.00 23 | 2005-04-17 19:18:06+08 | 01.2010100.00004 | 1 | = 200000.00 24 | 2005-04-17 21:45:31+08 | 01.2010100.00002 | 1 | = 9000.00 25 | 2005-04-17 22:16:08+08 | 01.2010100.00006 | 1 | = 100000.00 =20 -------------------------------------------------------------------------= ------------------------------------------------ CREATE TABLE "public"."transactions" ( "id" SERIAL,=20 "trx_timestamptz" TIMESTAMP(0) WITH TIME ZONE DEFAULT = ('now'::text)::timestamp(6) with time zone NOT NULL,=20 "account" CHAR(16) NOT NULL,=20 "trx_type_id" INTEGER NOT NULL,=20 "amount" NUMERIC(15,2) DEFAULT 0 NOT NULL,=20 "uid" INTEGER NOT NULL,=20 CONSTRAINT "transactions_pkey" PRIMARY KEY("id") ) WITH OIDS; -------------------------------------------------------------------------= ------------------ =20 and transaction type : =20 id | trx_name | t_type ----+----------+-------- 1 | Credit | CR 2 | Debet | DB =20 --------------------------------------------------------------------- CREATE TABLE "public"."trx_type" ( "id" SERIAL,=20 "trx_name" VARCHAR(32),=20 "t_type" CHAR(2),=20 CONSTRAINT "trx_type_pkey" PRIMARY KEY("id"),=20 CONSTRAINT "trx_typei_check0" CHECK ((t_type =3D 'CR'::bpchar) OR = (t_type =3D 'DB'::bpchar)) ) WITH OIDS;=20 =20 --------------------------------------------------------------------- =20 so, i'm using this query: =20 SELECT trans.id, trans.trx_timestamptz, trans.account, trans.debet, trans.credit FROM ( SELECT transactions.id, transactions.trx_timestamptz, transactions.account, =20 CASE WHEN trx_type.t_type =3D 'DB' THEN transactions.amount ELSE 0 END AS debet, CASE WHEN trx_type.t_type =3D 'CR' THEN transactions.amount ELSE 0 END AS credit FROM transactions INNER JOIN trx_type ON (transactions.trx_type_id =3D trx_type.id) =20 ) AS trans =20 result from above query : =20 id | trx_timestamptz | account | debet | credit ----+------------------------+------------------+-----------+------------= - 3 | 2005-04-14 17:16:49+08 | 01.2010100.00002 | 0 | = 1000000.00 4 | 2005-04-14 17:17:12+08 | 01.2010100.00002 | 0 | = 50000.00 5 | 2005-04-14 17:17:12+08 | 01.2010100.00003 | 0 | = 600000.00 6 | 2005-04-14 17:17:47+08 | 01.2010100.00002 | 7000.00 | = 0 7 | 2005-04-16 00:32:50+08 | 01.2010100.00003 | 0 | = 20000.00 11 | 2005-04-16 02:45:06+08 | 01.2010100.00002 | 0 | = 100000.00 12 | 2005-04-16 02:46:02+08 | 01.2010100.00002 | 0 | = 20000.00 13 | 2005-04-16 02:46:59+08 | 01.2010100.00002 | 163000.00 | = 0 14 | 2005-04-16 02:50:17+08 | 01.2010100.00005 | 0 | = 100000.00 15 | 2005-04-16 02:53:42+08 | 01.2010301.00001 | 0 | = 100000.00 16 | 2005-04-16 02:57:22+08 | 01.2010100.00001 | 0 | = 2000000.00 17 | 2005-04-16 23:56:44+08 | 01.2010200.00000 | 0 | = 10000000.00 18 | 2005-04-17 18:58:57+08 | 01.2010100.00003 | 0 | = 100000.00 19 | 2005-04-17 19:13:05+08 | 01.2010100.00002 | 0 | = 100000.00 20 | 2005-04-17 19:13:45+08 | 01.2010100.00002 | 0 | = 200000.00 21 | 2005-04-17 19:15:36+08 | 01.2010100.00002 | 0 | = 50000.00 22 | 2005-04-17 19:17:17+08 | 01.2010100.00005 | 10000.00 | = 0 23 | 2005-04-17 19:18:06+08 | 01.2010100.00004 | 0 | = 200000.00 24 | 2005-04-17 21:45:31+08 | 01.2010100.00002 | 0 | = 9000.00 25 | 2005-04-17 22:16:08+08 | 01.2010100.00006 | 0 | = 100000.00 =20 =20 my problem, i would like to place balance in last field. i try using = this query : =20 SELECT trans.id, trans.trx_timestamptz, trans.account, trans.debet, trans.credit, =20 (SELECT SUM(coalesce(credit,0)-coalesce(debet,0)) FROM ( SELECT transactions.id, transactions.trx_timestamptz, transactions.account, =20 CASE WHEN trx_type.t_type =3D 'DB' THEN transactions.amount ELSE 0 END AS debet, CASE WHEN trx_type.t_type =3D 'CR' THEN transactions.amount ELSE 0 END AS credit FROM transactions INNER JOIN trx_type ON (transactions.trx_type_id =3D trx_type.id) =20 ) sub WHERE (sub.id <=3D trans.id) AND (sub.account =3D trans.account) ) AS balance =20 FROM ( SELECT transactions.id, transactions.trx_timestamptz, transactions.account, =20 CASE WHEN trx_type.t_type =3D 'DB' THEN transactions.amount ELSE 0 END AS debet, CASE WHEN trx_type.t_type =3D 'CR' THEN transactions.amount ELSE 0 END AS credit FROM transactions INNER JOIN trx_type ON (transactions.trx_type_id =3D trx_type.id) =20 ) AS trans =20 ----- =20 everything is ok, but when record > 1000000 that query eat all my cpu = process and take a long time, i have wait for 3 mimutes but query = doesn't finish. (pgsql-8.0-1 running on Dual Xeon 2.8 and 2GB of RAM) =20 can you help me how to fix them. =20 Thanks =20 =20 =20 =20 =20 ------_=_NextPart_001_01C54562.EDAA9C97 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
      =20

Hi,

 

   Using a table with = larger data in=20 the sub-query always eats up CPU time and it is time consuming. The = below given=20 statement uses the transaction table two times in the sub-query and for=20 processing every record, it have to go through the same table twice in = the=20 sub-query.  It is better = to write a=20 function to process the data and return back the record=20 set.

 

Regards,

R.Muralidharan

         =20
-----Original Message-----
From: Muhyiddin A.M = Hayat=20 [mailto:middink@indo.net.id]
Sent: Tuesday, April 19, 2005 = 10:18=20 PM
To: pgsql-sql@postgresql.org
Subject: [SQL]=20 Debet-Credit-Balance Calculation

Dear All,

I have problem to = calculation=20 balance from debet and credit.

my transaction = table:
 
 id |   =20 trx_timestamptz     |    =20 account      | trx_type_id |  =20 = amount
----+------------------------+------------------+-------------+= -------------
 =20 3 | 2005-04-14 17:16:49+08 | 01.2010100.00002=20 |           1 = | =20 1000000.00
  4 | 2005-04-14 17:17:12+08 | 01.2010100.00002=20 |           1=20 |    50000.00
  5 | 2005-04-14 17:17:12+08 |=20 01.2010100.00003 = |          =20 1 |   600000.00
  6 | 2005-04-14 17:17:47+08 |=20 01.2010100.00002 = |          =20 2 |     7000.00
  7 | 2005-04-16 = 00:32:50+08 |=20 01.2010100.00003 = |          =20 1 |    20000.00
 11 | 2005-04-16 02:45:06+08 |=20 01.2010100.00002 = |          =20 1 |   100000.00
 12 | 2005-04-16 02:46:02+08 |=20 01.2010100.00002 = |          =20 1 |    20000.00
 13 | 2005-04-16 02:46:59+08 |=20 01.2010100.00002 = |          =20 2 |   163000.00
 14 | 2005-04-16 02:50:17+08 |=20 01.2010100.00005 = |          =20 1 |   100000.00
 15 | 2005-04-16 02:53:42+08 |=20 01.2010301.00001 = |          =20 1 |   100000.00
 16 | 2005-04-16 02:57:22+08 |=20 01.2010100.00001 = |          =20 1 |  2000000.00
 17 | 2005-04-16 23:56:44+08 | = 01.2010200.00000=20 |           1 |=20 10000000.00
 18 | 2005-04-17 18:58:57+08 | 01.2010100.00003=20 |           1 = |  =20 100000.00
 19 | 2005-04-17 19:13:05+08 | 01.2010100.00002=20 |           1 = |  =20 100000.00
 20 | 2005-04-17 19:13:45+08 | 01.2010100.00002=20 |           1 = |  =20 200000.00
 21 | 2005-04-17 19:15:36+08 | 01.2010100.00002=20 |           1=20 |    50000.00
 22 | 2005-04-17 19:17:17+08 |=20 01.2010100.00005 = |          =20 2 |    10000.00
 23 | 2005-04-17 19:18:06+08 |=20 01.2010100.00004 = |          =20 1 |   200000.00
 24 | 2005-04-17 21:45:31+08 |=20 01.2010100.00002 = |          =20 1 |     9000.00
 25 | 2005-04-17 = 22:16:08+08 |=20 01.2010100.00006 = |          =20 1 |   100000.00
 
----------------------------------------------------------------= ---------------------------------------------------------
CREATE TABLE "public"."transactions" = (
 =20 "id" SERIAL,
  "trx_timestamptz" TIMESTAMP(0) WITH TIME ZONE = DEFAULT=20 ('now'::text)::timestamp(6) with time zone NOT NULL,
  = "account"=20 CHAR(16) NOT NULL,
  "trx_type_id" INTEGER NOT NULL, =
 =20 "amount" NUMERIC(15,2) DEFAULT 0 NOT NULL,
  "uid" INTEGER = NOT NULL,=20
  CONSTRAINT "transactions_pkey" PRIMARY KEY("id")
) WITH=20 OIDS;
----------------------------------------------------------------= ---------------------------
 
and transaction type :
 
 id | trx_name |=20 t_type
----+----------+--------
  1 | Credit   |=20 CR
  2 | Debet    | DB
 
----------------------------------------------------------------= -----
CREATE TABLE "public"."trx_type" (
  = "id" SERIAL,=20
  "trx_name" VARCHAR(32),
  "t_type" CHAR(2), =
 =20 CONSTRAINT "trx_type_pkey" PRIMARY KEY("id"),
  CONSTRAINT=20 "trx_typei_check0" CHECK ((t_type =3D 'CR'::bpchar) OR (t_type =3D=20 'DB'::bpchar))
) WITH OIDS;=20
 
----------------------------------------------------------------= -----
 
so, i'm using this = query:
 
SELECT
  trans.id,
 =20 trans.trx_timestamptz,
  trans.account,
 =20 trans.debet,
  trans.credit
FROM
  = (
   =20 SELECT
     =20 transactions.id,
     =20 transactions.trx_timestamptz,
     =20 transactions.account,
 
     =20 CASE
        WHEN = trx_type.t_type =20 =3D 'DB' = THEN
         =20 transactions.amount
       =20 ELSE
         =20 0
      END AS=20 debet,
     =20 CASE
      WHEN trx_type.t_type =3D 'CR'=20 THEN
         =20 transactions.amount
       =20 ELSE
         =20 0
      END AS = credit
   =20 FROM
     =20 transactions
      INNER JOIN trx_type ON=20 (transactions.trx_type_id =3D trx_type.id)
 
  ) AS trans
 
result from above query = :
 
 id |   =20 trx_timestamptz     |    =20 account      |   debet  =20 |  =20 = credit
----+------------------------+------------------+-----------+--= -----------
 =20 3 | 2005-04-14 17:16:49+08 | 01.2010100.00002=20 |         0 | =20 1000000.00
  4 | 2005-04-14 17:17:12+08 | 01.2010100.00002=20 |         0 = |   =20 50000.00
  5 | 2005-04-14 17:17:12+08 | 01.2010100.00003=20 |         0 |  =20 600000.00
  6 | 2005-04-14 17:17:47+08 | 01.2010100.00002=20 |   7000.00=20 |           = 0
  7 |=20 2005-04-16 00:32:50+08 | 01.2010100.00003=20 |         0 = |   =20 20000.00
 11 | 2005-04-16 02:45:06+08 | 01.2010100.00002=20 |         0 |  =20 100000.00
 12 | 2005-04-16 02:46:02+08 | 01.2010100.00002=20 |         0 = |   =20 20000.00
 13 | 2005-04-16 02:46:59+08 | 01.2010100.00002 | = 163000.00=20 |           = 0
 14 |=20 2005-04-16 02:50:17+08 | 01.2010100.00005=20 |         0 |  =20 100000.00
 15 | 2005-04-16 02:53:42+08 | 01.2010301.00001=20 |         0 |  =20 100000.00
 16 | 2005-04-16 02:57:22+08 | 01.2010100.00001=20 |         0 | =20 2000000.00
 17 | 2005-04-16 23:56:44+08 | 01.2010200.00000=20 |         0 | = 10000000.00
 18=20 | 2005-04-17 18:58:57+08 | 01.2010100.00003=20 |         0 |  =20 100000.00
 19 | 2005-04-17 19:13:05+08 | 01.2010100.00002=20 |         0 |  =20 100000.00
 20 | 2005-04-17 19:13:45+08 | 01.2010100.00002=20 |         0 |  =20 200000.00
 21 | 2005-04-17 19:15:36+08 | 01.2010100.00002=20 |         0 = |   =20 50000.00
 22 | 2005-04-17 19:17:17+08 | 01.2010100.00005 = | =20 10000.00 |           = 0
 23 | 2005-04-17 19:18:06+08 | 01.2010100.00004=20 |         0 |  =20 200000.00
 24 | 2005-04-17 21:45:31+08 | 01.2010100.00002=20 |         0 = |    =20 9000.00
 25 | 2005-04-17 22:16:08+08 | 01.2010100.00006=20 |         0 |  =20 100000.00
 
 
my problem, i would like to = place=20 balance in last field. i try using this query :
 
SELECT
  trans.id,
 =20 trans.trx_timestamptz,
  trans.account,
 =20 trans.debet,
  trans.credit,
 

 =20 (SELECT
     =20 SUM(coalesce(credit,0)-coalesce(debet,0))
   = FROM
 =20 (
    SELECT
     =20 transactions.id,
     =20 transactions.trx_timestamptz,
     =20 transactions.account,
 
     =20 CASE
        WHEN = trx_type.t_type =20 =3D 'DB' = THEN
         =20 transactions.amount
       =20 ELSE
         =20 0
      END AS=20 debet,
     =20 CASE
      WHEN trx_type.t_type =3D 'CR'=20 THEN
         =20 transactions.amount
       =20 ELSE
         =20 0
      END AS = credit
   =20 FROM
     =20 transactions
      INNER JOIN trx_type ON=20 (transactions.trx_type_id =3D trx_type.id)
 
  )  sub
   WHERE
    = (sub.id=20 <=3D trans.id)
    AND
    = (sub.account =3D=20 trans.account)
  )
  AS balance
 

FROM
  (
   =20 SELECT
     =20 transactions.id,
     =20 transactions.trx_timestamptz,
     =20 transactions.account,
 
     =20 CASE
        WHEN = trx_type.t_type =20 =3D 'DB' = THEN
         =20 transactions.amount
       =20 ELSE
         =20 0
      END AS=20 debet,
     =20 CASE
      WHEN trx_type.t_type =3D 'CR'=20 THEN
         =20 transactions.amount
       =20 ELSE
         =20 0
      END AS = credit
   =20 FROM
     =20 transactions
      INNER JOIN trx_type ON=20 (transactions.trx_type_id =3D trx_type.id)
 
  ) AS trans
 
-----
 
everything is ok, but when record > 1000000 that query eat all = my cpu=20 process and take a long time, i have wait for 3 mimutes=20 but query doesn't finish. (pgsql-8.0-1 running on Dual = Xeon 2.8=20 and 2GB of RAM)
 
can you help me how to fix them.
 
Thanks
 
 
 
 


 
------_=_NextPart_001_01C54562.EDAA9C97--