![]() |
|
|||||||
![]() |
|
Microsoft Access question?
|
![]() |
LinkBack | Thread Tools | ![]() |
Display Modes | ![]() |
|
|||
|
How can I write a query in Microsoft Access that references the row above it?
Specifically, I have a table that looks like this: Row 1: 7 Row 2: 1 Row 3: -3 Row 4: -6 Row 5: -2 Row 6: 9 Row 7: 10 I want to write a query that will calculate a running total, but will never go negative. So the results would be: Row 1: 7 Row 2: 8 Row 3: 5 Row 4: 0 Row 5: 0 Row 6: 9 Row 7: 19 |
| Links |
|
|
|
|||
|
You can always use a CASE when returning a value to ensure it does not go below 0. Assuming your total column is named totalcol:
SELECT CASE totalcol WHEN < 0 THEN 0 ELSE totalcol END AS totalcol FROM table; |
|
|||
|
You can do it, but it takes a little work. If you refer to http://support.microsoft.com/kb/290136, Microsoft shows how to create a running total. In short, you define a query with a specific sorting method, then define the running total field as the sum of all the records that come before the current one in the sort.
For this to work, you need a column on which to sort. If the only column in your DB is the data to sum, this method doesn't work. From MS's site: Field: RunTot: Format(DSum("Freight","Orders","[EmployeeID]<=" & [EmpAlias] & ""),"$0,000.00") Total: Expression Show: Yes Here the field assumes the query is sorted by EmployeeID. To clip at 0, make a third field that says if RunTot <0, then 0. I don't have access available to check the syntax for if statements in query fields, so you have to do that yourself. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|