Solved

Filling out a date range with values based on most recent

Posted on 2014-12-01
5
142 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property 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.

792 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