running total using sum does not work

zezo2021

Member
Local time
Today, 05:10
Joined
Mar 25, 2021
Messages
400
Hello
why the expression on the query not work
Thanks in Advance
 

Attachments

Define 'not work'. It's runs for me and produces a total exactly as what I expect it to based on the logic in it.

Instead of an explanation, I suggest you give me the actual data you expect that query to produce.
 
Thanks for reply
I need to running subtract not Total
the DB contains Acutial Data
1571
1572
1573

the first row should be 0
the second Should be 1
The third should be one

it's not accumulated total

I need to subtract the Total

I can do accumulated total using report running sum = over all
I don't need running sum
----------
I need running minus :)
 
Thanks for reply
I need to running subtract not Total
the DB contains Acutial Data
1571
1572
1573

the first row should be 0
the second Should be 1
The third should be one

it's not accumulated total

I need to subtract the Total

I can do accumulated total using report running sum = over all
I don't need running sum
----------
I need running minus :)
You need a field named StartHours then in the query you would use [NewHours]-[StartHours]
 
I need to subtract the first cell from the second and the second from the third etc
 
I need to subtract the first cell from the second and the second from the third etc
Can you give us some detail of what your data is about and the need of these calculations. Perhaps a screen shot of your relationships as well.
To me, your reference to cells smacks of at db designed like a spreadsheet.
 
Again, an explanation is not the best way to communicate this.

Show me exactly all the data you want the query to produce based on what is in the table you uploaded. If needed take a screenshot of the existing query and put the correct expected values where they should go.
 
مرة أخرى، التوضيح ليس هو أفضل طريقة للتواصل.

أظهر لي بالضبط كل البيانات التي تريد أن ينتجها الاستعلام بناءً على ما هو موجود في الجدول الذي قمت بتحميله. إذا لزم الأمر، التقط لقطة شاشة للاستعلام الحالي وضع القيم المتوقعة الصحيحة حيث يجب وضعها.
 

Attachments

  • Manual Added the number.png
    Manual Added the number.png
    75.9 KB · Views: 19
  • Manual Added the number.png
    Manual Added the number.png
    75.9 KB · Views: 19
You need to use the database you uploaded to demonstrate. The database you uploaded has no ID=21. So either upload that database or provide a screenshot using the existing one.
 
This is getting painful. The screenshot is of your table.

I need to know what you expect your query to return.
 
You want Subtract to be the result? You want the difference between consecutive records? This not really a "running" value.
This is not simple in Access. Review http://allenbrowne.com/subquery-01.html#AnotherRecord

Consider:
SELECT TableHours.ID, TableHours.NewHours, TableHours.DateAdd,
[NewHours] - Nz(DMax("NewHours","TableHours","[ID]<" & [ID]), NewHours) AS Minus
FROM TableHours;

SELECT TableHours.ID, TableHours.NewHours, TableHours.DateAdd,
[NewHours]-Nz((SELECT TOP 1 NewHours FROM TableHours AS Dupe WHERE Dupe.ID<TableHours.ID ORDER BY ID DESC),NewHours) AS Minus
FROM TableHours;

IDNewHoursDateAddMinus
1157112/31/20240
1415721/2/20241
2015731/3/20251
2115801/3/20257

The second version might be a little faster but both will perform slowly with large dataset.

Now in a report you could do a RunningSum on the Minus field.
 
Last edited:
You want Subtract to be the result? You want the difference between consecutive records? This not really a "running" value.
This is not simple in Access. Review http://allenbrowne.com/subquery-01.html#AnotherRecord

Consider:
SELECT TableHours.ID, TableHours.NewHours, TableHours.DateAdd,
[NewHours] - Nz(DMax("NewHours","TableHours","[ID]<" & [ID]), NewHours) AS Minus
FROM TableHours;

SELECT TableHours.ID, TableHours.NewHours, TableHours.DateAdd,
[NewHours]-Nz((SELECT TOP 1 NewHours FROM TableHours AS Dupe WHERE Dupe.ID<TableHours.ID ORDER BY ID DESC),NewHours) AS Minus
FROM TableHours;

IDNewHoursDateAddMinus
1157112/31/20240
1415721/2/20241
2015731/3/20251
2115801/3/20257

The second version might be a little faster but both will perform slowly with large dataset.

Now in a report you could do a RunningSum on the Minus field.


Thank you so much for your efforts
Great
Done
I test the first sentence of SQL and work correctly
(y)(y)(y)(y)(y)(y)(y)(y)
 

Users who are viewing this thread

Back
Top Bottom