I have two tables, policies and claims
policies<-data.table(policyNumber=c(123,123,124,125),
EFDT=as.Date(c(\"2
I think this does mostly what you want. I need to run so don't have time to add the policy with no claims and clean the columns up, but I think the difficult issues are addressed:
setkey(policies, policyNumber, EXDT)
policies[, EXDT2:=EXDT]
policies[claims[, list( policyNumber, lossDate, lossDate, claimNumber, claimAmount)], roll=-Inf]
# policyNumber EXDT EFDT EXDT2 lossDate claimNumber claimAmount
# 1: 123 2012-02-01 2012-01-01 2013-01-01 2012-02-01 1 10
# 2: 123 2012-08-15 2012-01-01 2013-01-01 2012-08-15 2 20
# 3: 123 2013-01-01 2012-01-01 2013-01-01 2013-01-01 3 20
# 4: 124 2013-10-31 2013-01-01 2014-01-01 2013-10-31 4 15
Also, note it is trivial to remove/highlight claims outside of policy dates from this result.