Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Help calculating differences in values based on Date in Access

Posted on 2014-02-22
Last Modified: 2014-02-23
I have a table in access that have ID, Values and Date.  I have sorted the data based on ID and Date.  I want another field that will show the difference between value of previous  date and the current date and so on. Here is my sample data and expected Value in the Difference column
ID      Value      Date      Difference
10024      7      6/10/2013      0
10024      6      6/17/2013      1
10024      6      6/24/2013      0
10024      6      7/1/2013      0
10024      6      7/8/2013      0
10024      6      7/15/2013      0
10024      7      7/22/2013      -1
10024      6      7/29/2013      1
10024      6      8/5/2013      0
10024      6      8/12/2013      0
10024      6      8/19/2013      0
10024      6      8/26/2013      0
10005      9      6/10/2013      0
10005      9      6/17/2013      0
10005      9      6/24/2013      0
10005      9      7/1/2013      0
10005      9      7/8/2013      0
10005      9      7/15/2013      0
10005      9      7/22/2013      0
10005      9      7/29/2013      0
10005      9      8/5/2013      0
10005      9      8/12/2013      0
10005      9      8/19/2013      0
10005      9      8/26/2013      0
Question by:fb1990
  • 4
  • 3
LVL 40

Expert Comment

ID: 39880184
Look at sample. There is NULL value if previous Date is missing, you can use nz function if you like to have 0 in this case

Author Comment

ID: 39880577
Hello als315,

This works, but when I changed the Table1 to reflect my actual table name in my database.  I get a enter parameter for ID and Date.

Can you please explain how to implement this to me?  The actual ID name in my database is cust_id

Please with implementation.

LVL 40

Accepted Solution

als315 earned 250 total points
ID: 39880590
There is nested query in last column. You should change also there Table1 to real name:

SELECT Table1.ID, Table1.Value, Table1.Date, (SELECT TOP 1 [T].[VALUE] FROM Table1 As T
Where (T.ID = Table1.ID AND T.Date < Table1.Date) ORDER BY [T].[Date] DESC)-[Table1].[Value] AS Datep
FROM Table1
ORDER BY Table1.ID DESC , Table1.Date;

I see fields Date and Value in your table - it was not very good idea to use reserved words as a field name.
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)


Author Comment

ID: 39880604
Worked like a charm!  Thank you so much!

Author Closing Comment

ID: 39880606
All I have to say is, Thank you!

Author Comment

ID: 39880620
One more question.  How can i populate the blank values with 0 in the Datep field?

Also for some reason my queries are running very slow... Would you have an idea why?
LVL 40

Expert Comment

ID: 39881564
... NZ((SELECT TOP 1 [T].[VALUE] FROM Table1 As T
Where (T.ID = Table1.ID AND T.Date < Table1.Date) ORDER BY [T].[Date] DESC), [Table1].[Value])  - [Table1].[Value] .....

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question