473,379 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Consolidating data and joining tables based on dates

I have 2 tables which I need to join. One holds account data based on policy number which is updated each month when a policy makes a payment the other has the policy history.

POLICYHISTORY

strPolicyNumber strCoverChoice datUpdateTimeStamp
336392BRPA P 01/08/2009
336392BRPA PX 27/05/2010
336392BRPA P5 12/07/2010

ACCOUNTS

Policy_Id MonthEndDate PremiumPaid
336392BRPA 31/08/2009 £7.98
336392BRPA 31/08/2009 £7.98
336392BRPA 30/09/2009 £7.98
336392BRPA 31/10/2009 £7.98
336392BRPA 30/11/2009 £7.98
336392BRPA 31/12/2009 £7.98
336392BRPA 31/01/2010 £7.98
336392BRPA 28/02/2010 £7.98
336392BRPA 31/03/2010 £7.98
336392BRPA 30/04/2010 £7.98
336392BRPA 31/05/2010 £7.98
336392BRPA 30/06/2010 £7.98
336392BRPA 31/07/2010 £7.98
336392BRPA 31/08/2010 £0.78
336392BRPA 31/08/2010 £0.00
336392BRPA 31/08/2010 £8.76
336392BRPA 10/09/2010 £8.76


I need to add the cover choice from the policy table for each entry in the Accounts table. The problem is that I need to get it to add the cover choice based on the month end date to give something that looks like this:

Policy_Id MonthEndDate PremiumPaid strCoverChoice
336392BRPA 31/08/2009 £7.98 P
336392BRPA 31/08/2009 £7.98 P
336392BRPA 30/09/2009 £7.98 P
336392BRPA 31/10/2009 £7.98 P
336392BRPA 30/11/2009 £7.98 P
336392BRPA 31/12/2009 £7.98 P
336392BRPA 31/01/2010 £7.98 P
336392BRPA 28/02/2010 £7.98 P
336392BRPA 31/03/2010 £7.98 P
336392BRPA 30/04/2010 £7.98 P
336392BRPA 31/05/2010 £7.98 PX
336392BRPA 30/06/2010 £7.98 PX
336392BRPA 31/07/2010 £7.98 P5
336392BRPA 31/08/2010 £0.78 P5
336392BRPA 31/08/2010 £0.00 P5
336392BRPA 31/08/2010 £8.76 P5
336392BRPA 10/09/2010 £8.76 P5


The cover choice changed on the 27/05/2010 and 12/07/2010 so all entries in the accounts table with a month end dates after these dates will have the corresponding cover choice. This is the first time I've used a forum like this so apologies if I've not followed the correct posting convention bu any help would be appreciated as I'm stumped..
Sep 27 '10 #1
3 1595
MMcCarthy
14,534 Expert Mod 8TB
So if I understand your problem correctly you have to match the MonthEndDate to the datUpdateTimeStamp. So we need to clarify the logic of what you are doing. In your example you have an August datUpdateTimeStamp of 01/08/2009. So you need to add the Cover Choice to all records in the history table after that date and before the date of the september update? If that is not the correct logic can you tell me what is.
Sep 27 '10 #2
The policy was first created on the 01/08/2009 with cover choice P. The cover choice then changed to PX on the 27/05/2010 and then changed again to P5 on the 12/07/2010. I need to add the cover choice to each entry in the accounts table based on what teh cover choice was at each month end date. Hope this explains it a bit more clearly.
Sep 27 '10 #3
MMcCarthy
14,534 Expert Mod 8TB
OK I'm not sure if I have the logic completely correct so I suggest running this on a copy of your database rather than the original.

This code presumes you have added the strCoverChoice field to table accounts. It's a stand alone function so you can run it from whereever.

Expand|Select|Wrap|Line Numbers
  1. Function temp()
  2. Dim rs As DAO.Recordset
  3. Dim strPolicy As String
  4. Dim dateFrom As Date, dateTo As Date
  5.  
  6.     With CurrentDb
  7.         Set rs = .OpenRecordset("SELECT * FROM POLICYHISTORY ORDER BY strPolicyNumber, datUpdateTimeStamp")
  8.         rs.MoveFirst
  9.  
  10.         Do Until rs.EOF
  11.             strPolicy = rs!strPolicy
  12.             dateFrom = rs!datUpdateTimeStamp
  13.  
  14.             rs.MoveNext
  15.  
  16.             If strPolicy = rs!strPolicy Then
  17.                 dateTo = rs!datUpdateTimeStamp
  18.             Else
  19.                 dateTo = Now()
  20.             End If
  21.  
  22.             rs.MovePrevious
  23.  
  24.             DoCmd.RunSQL "UPDATE ACCOUNTS SET strCoverChoice='" & rs!strCoverChoice & "' " & _
  25.                  "WHERE PolicyID='" & rs!strPolciyNumber & "' " & _
  26.                  "AND MonthEndDate BETWEEN #" & dateFrom & "# AND #" & dateTo & "#"
  27.  
  28.             rs.MoveNext
  29.         Loop
  30.  
  31. End Function
Sep 27 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ciar?n | last post by:
I have about 7 tables I need to join, but am having a lot of difficulty with the joins, that I need some help on. I'll provide the details of four tables to illustrate the scenario. I have one...
2
by: James | last post by:
Can anyone please shed some light on the following... I have a framework that uses dynamically created tables, named using an incremental "attribute set ID", as follows: attrdata_1 attrdata_2...
2
by: narasingarao | last post by:
Hi to group, I'm a student of M.C.A. from B.I.T. Ranchi...I'm in my project period here i have to migrate the MS-Access database table to Oracle data base tables...so, please help me in getting...
10
by: Captain Nemo | last post by:
Hi I'm working on an ASP project where the clients want to be able to effectively perform SELECT queries joining tables from two different databases (located on the same SQL-Server). Does...
2
by: Gregc. | last post by:
G'day I have two tables with transactional data. Table 1 has a Cost Centre, Account, Fund Code and a YTD0-6 figure for each Account Number. Table 2 has Cost Centre, Account, Fund Code, Period...
1
by: EyesFriedOpen | last post by:
I was trying to post this to the IBM DB2 forum, however I do not have the "New Post" button available when I view the IBM DB2 forum. Therefore, I apologize if this is not the right place for this. ...
1
by: nights | last post by:
hi! i'm just a newbie with php and mysql... and i'm having problems joining tables from different databases... can you please help me? here's my query: SELECT database1.table1.field1,...
11
by: khushbubhalla | last post by:
how to compare data between tables and views row by row , column by column in the test and production environment
4
by: Cyprus106 | last post by:
Apparently, Im incapable of properly executing this query! I've been at this for far too long and gotten nowhere. Forgive me if this is a umb mistake; I'm not great at SQL. I've got three tables,...
2
by: dandalero | last post by:
Hi, I'm joining tables from 2 different databases instance located on different SQL Server, I tried server.database.owner.table but I got the following error: "Could not find server...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.