Calculations using data from more than one record in a table

Programmer John

New Member
Joined
Dec 19, 2012
Messages
2
I have a data table with many records each including an event date. I then use queries to extract and sort data that meets certain criteria. My question is how can I calculate the interval between the dates in successive records. What I need is to show the number of days since the previous event for each record extracted by the query. I know the ID numbers of the two records but I can't determine how to jump across record boundaries to make the calculation.

Is this possible?

Thanks.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Yes, but you need a couple of things to make it work.
1. A primary key (Autonumber is much easier than Text)
2. A calculated field in your query that is a query in its own right, pulling the previous value in the current record
3. A third field that identifies the project, if your intervals are based on (say) sales campaigns

Note: You can't use this query for data entry.
Assuming that you have an EventDate field and an EventID primary key, the SQL would be --
Code:
SELECT Events.EventID, Events.EventDate, (SELECT T.EventDate FROM Events AS T WHERE T.EventID=(SELECT Max(EventID) FROM Events AS E WHERE E.EventID < Events.EventID)) AS LastEvent, [EventDate]-[LastEvent] AS [Interval]
FROM Events;

Denis
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,103
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top