473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I write an SQL in Access to compare data in the previous row to return a value

1
I am using the following sql query via ODBC to pull data from a database into Access.
Expand|Select|Wrap|Line Numbers
  1. SELECT table.excav_id,
  2.        table.RecordID, 
  3.        table.indx_dpr, 
  4.        table.tons_bkt, 
  5.        table.tons_trk
  6. FROM table
  7. WHERE (((table.Record_Date)>=#8/28/2008 6:15:00#))
  8. ORDER BY table.excav_id, table.RecordID, table.indx_dpr,table.Record_Date;
In the query result below, the value in the column ‘tons_trk’ is the running total of the ‘tons_bkt’ for loading a truck. The column ‘Load_#’ does not exist in the dataset, but it is what I would like to populate to identify each individual load. It can just be a sequencial value as indicated..

The way to identify the total segment for a single load would be to compare the value in the previous record "tons_trk" to the current record "tons_trk" and if it is less than, start a new load_#. I just do not know how to write the sql query to accomplish this.
Expand|Select|Wrap|Line Numbers
  1. RecordID    excav_id    indx_dpr    tons_bkt    tons_trk    load_#
  2. 1923367    1    1    54    54    1
  3. 1923368    1    2    56    110    1
  4. 1923369    1    3    54    165    1
  5. 1923370    1    4    48    213    1
  6. 1923371    1    5    39    252    1
  7. 1923372    1    1    68    68    2
  8. 1923373    1    2    63    131    2
  9. 1923374    1    3    58    189    2
  10. 1923375    1    4    55    243    2
  11. 1923376    1    1    65    65    3
  12. 1923377    1    2    67    133    3
  13. 1923378    1    3    69    202    3
  14. 1923379    1    4    61    263    3
  15. 1923380    1    1    34    34    4
  16. 1923381    1    2    0    34    4
  17. 1923382    1    3    0    34    4
  18. 1923383    1    1    72    72    5
  19. 1923384    1    2    60    132    5
  20. 1923385    1    3    52    184    5
  21. 1923386    1    4    67    252    5
  22. 1923387    1    2    62    62    6
  23. 1923388    1    3    58    121    6
  24. 1923389    1    4    56    177    6
  25. 1923390    1    5    54    231    6
Thanks
Sep 3 '08 #1
1 2358
puppydogbuddy
1,923 Expert 1GB
The following links discuss and give examples of 3 methods you can use:
1.DLookup
2.User Defined Function (UDF)
3.Subquery

http://support.microsoft.com/kb/210504
http://allenbrowne.com/subquery-01.html#AnotherRecord
Sep 7 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
49
by: Relaxin | last post by:
It is just me or has MS created some of the worst ways to access and display data? You can use a DataSet, but if you want to sort or filter the data to must use a DataView which is created from...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
18
by: jacob navia | last post by:
In C, we have read-only memory (const), read/write memory (normal data), and write only memory. Let's look at the third one in more detail. Write only memory is a piece of RAM that can only...
13
by: Shelley | last post by:
Compare Current Year Worksheet with Previous Year Worksheet and if SSN exists in Current Year Worksheet & Not in Previous Year - Copy this Row from Current Year Worksheet & Paste into Previous Year...
9
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result...
3
by: super.raddish | last post by:
Greetings, I am relatively new to, what I would call, advanced XSLT/XPath and I am after some advice from those in the know. I am attempting to figure out a mechanism within XSLT to compare the...
1
MMcCarthy
by: MMcCarthy | last post by:
Access has a number of built-in functions which can be generally used in queries or VBA code. Some of the more common ones are: Note: anything in square brackets is optional Date Functions ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.