473,804 Members | 4,795 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 2380
puppydogbuddy
1,923 Recognized Expert Top Contributor
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
6826
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 elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
49
3217
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 a DataSet. But, if you sort by using the Grid (clicking the header) you can no longer use the DataSet (or maybe the DataView, if that is what you are using) to locate the record that the user has selected!!
18
4897
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) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
14
5424
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 7.2 environment, the choices the optimizer makes often seem flaky. But this last example really floored me. I was hoping someone could explain why I get worse response time when the optimizer uses two indexes, than when it uses one. Some context:
18
3717
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 be written to, since its contents are undefined. The program is allocating a new piece of data, and the previous contents aren't relevant. This memory
13
6967
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 Worksheet Compare Previous Year Worksheet with Current Year Worksheet and if SSN exists in Previous Year Worksheet & Not in Current Year Worksheet - Delete this Row out of Previous Year Worksheet - THIS IS WHERE I'M HAVING TROUBLE. This is the...
9
3949
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 - I have read every post out there and spent hours trying to figure out the problem with no success whatsoever - I have constrained the problem to one form however, and I think it's hiding somewhere in my code associated with this form, which is...
3
9618
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 difference between two source documents and output node-sets which are "different" (changed or new) to new XML files using xsl:result-document To describe the problem I have provided some example data below along with my a portion of my current...
1
19413
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 Date() - Returns the current system date Now() - Returns the current system timestamp (date and time)
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.