Go Back   Talk Microsoft > Microsoft Operating Systems & Software > Microsoft Office Family

Reply
     Microsoft Access question?  
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-26-2007, 10:19 PM
Junior Member
 
Join Date: Nov 2007
Posts: 6
Default Microsoft Access question?

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
Reply With Quote
Links
  #2 (permalink)  
Old 11-26-2007, 11:14 PM
Member
 
Join Date: Sep 2007
Posts: 67
Default

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;
Reply With Quote
  #3 (permalink)  
Old 11-26-2007, 11:38 PM
Junior Member
 
Join Date: Nov 2007
Posts: 4
Default

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.
Reply With Quote
  #4 (permalink)  
Old 11-26-2007, 11:52 PM
Junior Member
 
Join Date: Nov 2007
Posts: 2
Default

I guess you cant use the CASE statement in MS Access.
try this.

Select IIF (total < 0,0,total) from table

hope this help..
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:14 AM.