20. februar 2016 - 10:31Der er
26 kommentarer og 1 løsning
Query der beregner saldo løbende
Jeg har en tabel med en masse kontotransaktioner hvor jeg som det er nu har noget kode der løber alle poster igennem sorteret på transaktionsdato og indtastningsdato. Koden tager så debit beløb, trækker kredit fra og gemmer resultatet i saldo felt. Det gør den så hele vejen igennem alle transaktioner. Men som der bliver flere og flere transaktioner i tabellen, så bliver det mere og mere tidskrævende. Jeg tænker så på om man ikke kan skrue et par queries sammen der kan løse dette så jeg kan spare den opdatering i tabellen.
What I do now is what you write in #1, but if I the have to insert a transaction from 2 days ago I still have to recalc the saldo after the new record in order to get it right. I have used many hours thinking of what I could do to get it running faster and easier. But haven't 'fundet de vise sten' :-) It is a Delphi program that runs as frontend to the database and a lot of the data updates are done by tasks running in the background. but this one here I have had no success with. So now I am back to a query.
I can see which fields are debit and credit and saldo, but how do I sort? In query I can see that fldDate is used to sort, can I use this and is it unique? If not then how do I know correct order of transactions?
Is there anything else I need to take into concideration?
Just want to make sure I'm calculating correctly. In my heasd debit is minus and credit is plus, so if I assume that is correct I add the debit and credit fields then add result to saldo from previous transaction.
SELECT T1.flddate AS DT, T1.fldcreated_date AS CDT, [flddebit]*-1 AS DEB, T1.fldkredit AS KRE, [DEB]+[KRE] AS RowSum FROM tblfs_transactions AS T1 ORDER BY T1.flddate, T1.fldcreated_date;
Then make another query (qrySaldo) SELECT T.DT, T.CDT, T.DEB, T.KRE, (SELECT SUM(RowSum) FROM qryfs_transactions QF WHERE QF.DT <= T.DT AND QF.CDT <= T.CDT) AS Saldo FROM qryfs_transactions AS T ORDER BY T.DT, T.CDT;
The saldo isn't calculated correct I have put a new one in the dropbox and if you open qryfs_saldo and loops the records you will see several times where saldo isn't correct. The last record and some where there is drawn 4000 It has something to do with the dates I am sure. The 4000 are some fixed expenses that are created maybe once or twice a year and they make something strange happen
Hugo, can you give me a bit more information as to how I find the incorrectly calculated saldo? There are 732 records so dont want to have to manually calculate each. Also an explanation as to why they are not correct.
In the VBA code which currently calculates there must be some logic which knows when and how to make the calculation.
So as much information as possible to help me understand how to make calculation.
If you look at records with 4000 kredit you will find the wrong calc
It is actually not VBA that I use today but Delphi I loop the table sorted by flddata, fldcreated_date and the I calc each record and saves the result back to the table.
SELECT T.DT, T.CDT, T.DEB, T.KRE, T.RowSum, (SELECT SUM(RowSum) FROM qryfs_transactions QF WHERE QF.DT < T.DT OR (QF.DT = T.DT AND QF.CDT <= T.CDT) ) AS Saldo FROM qryfs_transactions AS T ORDER BY T.DT, T.CDT;
I have just tried the latest query on a full set of data and it looks like it is about 9000 of course I have exported my data to Excel and it is the same. The query is about 9000 of course I will try to analyze a bit more and try to find out where it goes wrong.
I have found the 'error' I think There was one row with data where there was a null value in kredit field. That messed everything up. Make an answer for well earned points.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.