Filling out a date range with values based on most recent

Posted on 2014-12-01
Medium Priority
Last Modified: 2014-12-01
I have a table like this:
Date     Value
1/1           3
5/1           5
7/1           8

And another like this:

I want to combine these tables into this
Date  Value
1/1    3
2/1    3
3/1    3
4/1    3
5/1    5
6/1    5
7/1    8
8/1    8

So in other words, it's like a left join from tblDates on tblValues where nulls yield that last (i.e. most recent) change value.  Restated, I want to know what change value is in effect for each date in the list.  If nothing matches, take the most recent.

This could be done with Domain functions (like DLast), but that's very cumbersome.  Is there a better way?
Question by:shacho
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
  • 3
LVL 24

Expert Comment

by:Phillip Burton
ID: 40473287
It may be cumbersome, but it's probably the best way.

If this was SQL Server, then you could use the LAG function, but that's not present in Access.

Outside of DLast, you would have to do some JOINs with non-equal comparisons, and if you have a big table, that would get slow - so Domain functions are probably the best for Access.
LVL 24

Accepted Solution

Phillip Burton earned 2000 total points
ID: 40473343
Here's the SQL for the query with non-equal comparisons. It might not be as slow as I was fearing:

SELECT qryRelevantDate.Date, tblChanges.Value
FROM (SELECT tblDates.Date, Max(tblChanges.Date) AS MapDate
FROM tblDates INNER JOIN tblChanges ON tblDates.Date >= tblChanges.Date
GROUP BY tblDates.Date) as qryRelevantDate INNER JOIN tblChanges ON qryRelevantDate.MapDate = tblChanges.Date;

Open in new window

LVL 24

Expert Comment

by:Phillip Burton
ID: 40473344
This is the output:

Date	Value
01/01/2014	3
02/01/2014	3
03/01/2014	3
04/01/2014	3
05/01/2014	5
06/01/2014	5
07/01/2014	8
08/01/2014	8

Open in new window

LVL 51

Expert Comment

by:Gustav Brock
ID: 40473443
Why not pick the values directly:

    (Select Max(tblChanges.Value)
    From tblChanges
    Where tblChanges.Date <= tblDates.Date) As [Value]


Author Comment

ID: 40473920
Yep, that does the trick, Phillip.
Thanks all for your comments!


Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Suggested Courses

801 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