Subject RE: [firebird-support] need advice in query...
Author Svein Erling Tysvær
As Alexandre said, doing things on the client could be the best solution. An alternative if you want to do it on the server (there are lots of unknown factors that could make you prefer one or the other), could be

SELECT RECURSIVE SUM_MUTASI(ACCOUNT, ID, DEBET, CREDIT, SALDO)
AS (SELECT M.ACCOUNT, M.ID, M.DEBIT, M.CREDIT, COALESCE(M.DEBET, 0)-COALESCE(M.CREDIT, 0)
FROM MUTASI M
WHERE NOT EXISTS(SELECT * FROM MUTASI M2
WHERE M2.ID < M.ID
AND M2.ACCOUNT = M.ACCOUNT)
UNION ALL
SELECT SM.ACCOUNT, M.ID, M.DEBET, M.CREDIT, SM.SALDO+COALESCE(M.DEBET, 0)-COALESCE(M.CREDIT, 0)
FROM SUM_MUTASI SM
JOIN MUTASI M ON SM.ACCOUNT = M.ACCOUNT
AND SM.ID < M.ID+0
WHERE NOT EXISTS(SELECT * FROM MUTASI M2
WHERE M2.ID+0 > SM.ID
AND M2.ID+0 < M.ID
AND M2.ACCOUNT = M.ACCOUNT))
SELECT *
FROM SUM_MUTASI
ORDER BY ACCOUNT, ID

There is a maximum depth of recursion, I wonder whether it could be as small as 1024, so it will not work for long if you want a running total for the entire table and the table is more than a few days/weeks old (assuming 50-200 new records per day).

HTH,
Set

-----Original Message-----
From: firebird-support@yahoogroups.com [mailto:firebird-support@yahoogroups.com] On Behalf Of send2iwan
Sent: 10. november 2009 07:03
To: firebird-support@yahoogroups.com
Subject: [firebird-support] need advice in query...

hi all,

i want to make query like this.

table name=mutasi

id transdate debet kredit saldo
== ========== ===== ====== =====
10 10/01/2009 10000 10000
20 10/05/2009 250 10250
30 10/30/2009 5000 5250

how to make the query espesialy for field saldo?
everyday records increase 50-200 records.

thanks.
Iwan




------------------------------------

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Visit http://www.firebirdsql.org and click the Resources item
on the main (top) menu. Try Knowledgebase and FAQ links !

Also search the knowledgebases at http://www.ibphoenix.com

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Yahoo! Groups Links