-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
> command in ms-sql 7 can use calculate field (column) balance from id=1
> to id=4: "update xx set bal=balance=bal+debet-credit"
You cannot do such a thing in SQL alone: you must use a procedural
language. One way is with plpgsql:
CREATE OR REPLACE FUNCTION makebalance() RETURNS VARCHAR LANGUAGE 'plpgsql' AS
'
DECLARE total INTEGER := 0; myrow RECORD;
BEGIN
FOR myrow IN SELECT id, credit-debit AS cd FROM xx ORDER BY id ASC LOOP total := total + myrow.cd; UPDATE xx SET
balance= total WHERE id=myrow.id;
END LOOP;
RETURN ''Final balance: '' || total;
END;
';
This is just a rough idea. You may have to create the language first:
see the "createlang" script in the bin directory of your PostgreSQL installation.
- --
Greg Sabino Mullane greg@turnstep.com
PGP Key: 0x14964AC8 200302101601
-----BEGIN PGP SIGNATURE-----
Comment: http://www.turnstep.com/pgp.html
iD8DBQE+SBN9vJuQZxSWSsgRAoNEAJ9IKmRW6IlHu12x2w7i0G6THB7oGACgi7lM
bIyssS1GQRgY0aQFRzyQKl8=
=ArPb
-----END PGP SIGNATURE-----