D
dje
I am trying to calculate a running total in a query. The fields are date and
net change in balance from which I want to create a bank statement like
running balance for the forseeable future certainly several years. My initial
enquiries indicated the way to go was using datepart and dsum. The resultant
code was:
SELECT [Metal Daily Prices Table].MyDate, [Cash - USD Trans Entries
Query].[Tot Bal USD Equiv], NZ(DatePart("yyyy",[Metal Daily Prices
table].Mydate),0) AS Ayear, Nz(DatePart("m",[Metal Daily Prices
table].Mydate),0) AS Amonth, NZ(DatePart("d",[Metal Daily Prices
table].Mydate),0) AS Aday, CDbl(NZ(DSum("[Tot Bal USD Equiv]","[Cash - USD
Trans Entries Query]","Datepart('d',[Mydate])<=" & [Aday] & "And
DatePart('m',[Mydate])<=" & [Amonth] & "And Datepart('yyyy',[Mydate])<=" &
[ayear] & ""),0)) AS Runtot
FROM [Metal Daily Prices Table] LEFT JOIN [Cash - USD Trans Entries Query]
ON [Metal Daily Prices Table].MyDate = [Cash - USD Trans Entries
Query].[Value Date]
ORDER BY [Metal Daily Prices Table].MyDate, NZ(DatePart("yyyy",[Metal Daily
Prices table].Mydate),0), Nz(DatePart("m",[Metal Daily Prices
table].Mydate),0), NZ(DatePart("d",[Metal Daily Prices table].Mydate),0);
The code works for one month but when a second month is introduced the
running total reverts to zero and the running total increases in a strange
way i.e. 1/3/07 total is aggregate of 1/2/07 plus 1/3/07, 2/3/07 total is
aggregate of 2/2/07 plus 2/3/07 etc so by 31/3/07 the running total is the
aggregate of the whole of Feb & Mar (but half way through March it does not
include the second half of February amounts)
Am I taking the right approach and if so does anyone know how to get the
correct result? I am a novice so please bear this in mind with any
solutions!!.Thanks
net change in balance from which I want to create a bank statement like
running balance for the forseeable future certainly several years. My initial
enquiries indicated the way to go was using datepart and dsum. The resultant
code was:
SELECT [Metal Daily Prices Table].MyDate, [Cash - USD Trans Entries
Query].[Tot Bal USD Equiv], NZ(DatePart("yyyy",[Metal Daily Prices
table].Mydate),0) AS Ayear, Nz(DatePart("m",[Metal Daily Prices
table].Mydate),0) AS Amonth, NZ(DatePart("d",[Metal Daily Prices
table].Mydate),0) AS Aday, CDbl(NZ(DSum("[Tot Bal USD Equiv]","[Cash - USD
Trans Entries Query]","Datepart('d',[Mydate])<=" & [Aday] & "And
DatePart('m',[Mydate])<=" & [Amonth] & "And Datepart('yyyy',[Mydate])<=" &
[ayear] & ""),0)) AS Runtot
FROM [Metal Daily Prices Table] LEFT JOIN [Cash - USD Trans Entries Query]
ON [Metal Daily Prices Table].MyDate = [Cash - USD Trans Entries
Query].[Value Date]
ORDER BY [Metal Daily Prices Table].MyDate, NZ(DatePart("yyyy",[Metal Daily
Prices table].Mydate),0), Nz(DatePart("m",[Metal Daily Prices
table].Mydate),0), NZ(DatePart("d",[Metal Daily Prices table].Mydate),0);
The code works for one month but when a second month is introduced the
running total reverts to zero and the running total increases in a strange
way i.e. 1/3/07 total is aggregate of 1/2/07 plus 1/3/07, 2/3/07 total is
aggregate of 2/2/07 plus 2/3/07 etc so by 31/3/07 the running total is the
aggregate of the whole of Feb & Mar (but half way through March it does not
include the second half of February amounts)
Am I taking the right approach and if so does anyone know how to get the
correct result? I am a novice so please bear this in mind with any
solutions!!.Thanks