Sum the all the above rows of a field on another field in access query

Sivasan
Sivasan used Ask the Experts™
on
I have an access table with field ID, Item NoofHour this in sequence I like the keep summing the NoofHours
so table Order
id  Item  Hours
1    abc    5
2    ccc    10
3    yyy    2
4   kka     3

I want to make query so result are
id  Item  Hours   Totalhr
1    abc    5             5                
2    ccc    10           15                
3    yyy    2             17
4   kka     3             20

So add the above hours. I will appreciate if somebody can tell me how I can get  sum  of row above.
Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Sivasan,

I think you are describing a "running total". Can you share the query that you have written so far?

- Paul N.
Distinguished Expert 2017

Commented:
A running total is a trivial process in a report.  You just use the RunningSum() property to create the value.

However, it is much more of a problem in a query and it is also very inefficient and so may be extremely slow.  The most efficient method is to do a self- join and sum all the rows with a PK <= the current one.  This of course only works if you actually have a numeric uniqueID that is ascending.
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
Here is how:

Select *, 
    (Select Sum(Hours) From [Orders] As T 
    Where T.Id <= [Orders].Id) As TotalHours
From [Order]

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Hi Gustav Brock,
Thank you very much. This is what I was looking for worked
Thank you
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
You are welcome!
Distinguished Expert 2017

Commented:
Thanks for providing the fish Gus just in case the OP couldn't figure out my instructions.
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
You are welcome!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial