Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / May 2005

Tip: Looking for answers? Try searching our database.

Calculate the difference between records

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nats - 05 May 2005 23:53 GMT
Hi,

I have a table that has an autonum primary key and fields of date, meterID,  
reading and volume. What I would like is for the volume field to be the
result of a calculated field of the latest reading for a particular meterID
minus the previous meter reading for that same meterID (think water use). I
assume this can be done via an update query (?) if the user fills in the
meterID, date and reading, but I am getting rather confused.

Hopefully someone can shed some light on this. Any help greatly appreciated.

Thanks
Jack MacDonald - 06 May 2005 04:02 GMT
I have used two approaches - one faster to execute, but more complex
(or impossible)  to set up. The other is easier to understand, but
slower to execute.

Fast execution approach - this involves creating a "self-join" in
which the table is joined to itself, but that record #n is related to
record #n-1. How? that's the trick. Your database structure must
include some information that will reliablly relate one record to the
previous one. You can use the autonumber PK **IF** you can safely
assume that sequential records will have sequential PK's -- not always
a safe assumption. You create the query in the conventional manner in
the grid by adding the table twice and joining the PK fields. Then
switch to SQL view and manually chage the "=" relationship in the JOIN
clause from table1.PKfield = Table2.PKfield   to   table1.PKfield =
table2.PKfield-1

Second approach is to add a computed field to your single-table query
using the DMax() function to find the record that has the latest date
that is LESS than the value of the current record.

[aircode]

SELECT PKField, DateField, VolDifference: VolField -
DMax("volField","VolumeTableName","DateField<" & [DateField])

Easier to construct than the first approach, but it slows down
significantly when there are many records in the database.

>Hi,
>
[quoted text clipped - 8 lines]
>
>Thanks

**********************
jackmacMACdonald@telusTELUS.net
remove uppercase letters for true email
http://www.geocities.com/jacksonmacd/ for info on MS Access security
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.