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

pbornemeier

Well-known Member
Joined
May 24, 2005
Messages
3,910
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

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
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,214,375
Messages
6,119,165
Members
448,870
Latest member
max_pedreira

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