Query Expression Help: Calculating Date Differences For One Field

drhopkins

New Member
Joined
Sep 4, 2013
Messages
5
So I have a table that is pretty simple. It has [ID], [Shot] and I need to calculate the difference in days between each shot for each grouped ID.

I was trying to use DLookUp in SQL(among a few other ways) but it wasn't working for me. I wouldn't say I'm even close to an experienced user, but I do understand a lot of basics and have created a database for my research(albeit using wizards, details form, and the expression builder:LOL:). That being said, some of the links that I found on previous articles just wasn't working for me. After a few days of tinkering with it on and off, I felt like it was time to post this.

Here's an example where the first two fields are what I want to use in the query, and my desired output for each record. The ID's are all structure like shown. In design view I have it grouped by ID using Totals.

IDShotValue Desired
JB01239/18/130
JB01239/20/132
JB01239/29/139
AB01017/1/130
AB01017/8/137

<tbody>
</tbody>


I hope that is enough clarification, but I would be willing to provide a lot more. Thanks in advance :)
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
You need a subquery to reference a previous row in Access.
I took your data and assumed that the table was called Photos. Here is the output of the query: (Dumped into Excel, then pasted here)
Excel 2010
EFGH
1IDShotPrevShotInterval
2AB010101-Jul-130
3AB010108-Jul-1301-Jul-137
4JB012318-Sep-130
5JB012320-Sep-1318-Sep-132
6JB012329-Sep-1320-Sep-139

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1


Here is the SQL for that query:
Code:
SELECT Photos.ID, Photos.Shot, (SELECT TOP 1 Shot FROM Photos AS P WHERE P.ID = Photos.ID AND P.Shot < Photos.Shot ORDER BY ID, Shot DESC) AS PrevShot, IIf(IsNull([PrevShot]),0,[Shot]-[PrevShot]) AS [Interval]
FROM Photos
ORDER BY Photos.ID, Photos.Shot;

Some explanation:
To find the previous record in a table, you need to build a query where the table references itself. The second copy is given an alias (in this case P, the first letter of the original table) and you need to find the first record where Shot is earlier than the current date, and the IDs match. That's what the TOP 1 statement does; limit the result to just one record, from which you pull the Shot value.
Once you have the data lined up, you need an expression to return 0 if PreviousShot is null, otherwise subtract the dates.

Denis
 
Upvote 0
Great explanation Denis, thanks very much!

I inputted the data into the tables and changed to table references around, but I got this code [h=2]"At most, one record can be returned from this subquery"[/h]
SELECT tblVivi.ID, tblVivi.Shot, (SELECT TOP 1 Shot FROM tblVivi AS V WHERE V.ID = tblVivi.ID AND V.Shot < tblVivi.Shot ORDER BY ID, Shot DESC) AS PrevShot, IIf(IsNull([PrevShot]),0,[Shot]-[PrevShot]) AS [Interval]
FROM tblVivi
ORDER BY tblVivi.ID, tblVivi.Shot;

I know it has to do with the subquery finding multiple matches. At least Microsoft Access tips: Surviving subqueries says that is the most likely cause. I tried switching to the SELECT max option listed.

I'm wondering if there is anything inherently wrong with the code for Microsoft Access 2010 or if the problem is being generated from poor design
 
Upvote 0
I'd guess a design or syntax issue because I can't imagine that Access 2010 would have a special problem with the code.
I can't see an obvious issue with your SQL -- looks like a straight replacement of table names to me.

How many records are there in the table? I'll see if I can create a subquery to find where the multiples are occurring.

Denis
 
Upvote 0
Yea I don't think it looks wrong either. There is 396 total records. The table has two fields, ID and Shot. The table also has a 1-Many relationship with a table where information based on each ID is stored. Shot is set as a date/time field. In the design view of the query both ID and Shot are set to ascending sort. I tried fiddling with ascending, descending, and no shot to no avail.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,444
Messages
6,124,893
Members
449,194
Latest member
JayEggleton

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