SQL How to Retrieve Most Recent Data On or Before a Specified Date

pbornemeier

Well-known Member
Joined
May 24, 2005
Messages
3,915
I have a table with 2 columns: DateTime & Value, indexed by DateTime. A new row is created whenever a sensor's current value differs from the previous value. There are date gaps in the table.

What SQL could I use to extract the most recent value from the table that that is on or before a particular DateTime? For instance the table has entries for noon on each of 12 June 16, 14 June 16, 23 June 16 and 01 Aug 16; What was the value at 10:45 15 June 2016? Even though that DateTime is not in the table I want to retrieve the value associated with 1200 14 June.

Attempting this in Access 2010, but would appreciate any SQL's answer.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Something like this ought to do it:

SELECT
top 1 value
FROM
YourTableName
where
`DateTime` < date()
order by
`DateTime`desc


The above query uses the current date. For specific date use something like this:


SELECT
top 1 value
FROM
YourTableName
where
`DateTime` < #01/01/1970#
order by
`DateTime`desc
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,809
Messages
6,174,761
Members
452,582
Latest member
ruby9c

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