Solved

Filling out a date range with values based on most recent

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

And another like this:
tblDates
Date
1/1
2/1
3/1
4/1
5/1
6/1
7/1
8/1

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?
0
Comment
Question by:shacho
  • 3
5 Comments
 
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.
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 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

0
 
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

0
 
LVL 49

Expert Comment

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

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

/gustav
0
 

Author Comment

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

Cheers,
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

744 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now