Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Vinerc
Contributor II
Contributor II

Help with match with last date from another table

Hello all,

I'm trying to do something like approximate match with dates and can't figure out how. I have two separate tables:

Failures:

VehicleDate
101/03/2019
101/07/2019
212/11/2019
301/04/2019

 

Maintenances:

VehicleDate
114/12/2018
116/04/2019
117/08/2019
118/12/2019
201/01/2019
201/06/2019
201/11/2019
201/04/2020
301/02/2019
301/05/2019
301/08/2019
301/11/2019
301/02/2020

 

I want to create a column in failures to pick the last maintenance of that vehicle before that failure, so the final table would be:

Failures:

VehicleDateLast Maintenance
101/03/201914/12/2018
101/07/201916/04/2019
212/11/201901/11/2019
301/04/201901/02/2019

 

Thanks in advance.

Labels (4)
1 Solution

Accepted Solutions
lironbaram
Partner - Master III
Partner - Master III

hi 

using interval match function you could do it 

data:
load * inline [
Vehicle, Date
1 ,01/03/2019
1 ,01/07/2019
2,12/11/2019
3 ,01/04/2019
];

MaintenancesTemp:
NoConcatenate load * inline [
Vehicle ,Date
1, 14/12/2018
1 ,16/04/2019
1 ,17/08/2019
1, 18/12/2019
2 ,01/01/2019
2, 01/06/2019
2 ,01/11/2019
2, 01/04/2020
3 ,01/02/2019
3, 01/05/2019
3 ,01/08/2019
3, 01/11/2019
3 ,01/02/2020
];

maintenance:

load Vehicle,
Date as StartDate,
if (isnull(Previous(Date)),today(),Previous(Date)) as Enddate
resident MaintenancesTemp
order by Vehicle,Date Desc;

drop Table MaintenancesTemp;

inner join IntervalMatch(Date,Vehicle) load StartDate,Enddate ,Vehicle Resident maintenance;

View solution in original post

3 Replies
lironbaram
Partner - Master III
Partner - Master III

hi 

using interval match function you could do it 

data:
load * inline [
Vehicle, Date
1 ,01/03/2019
1 ,01/07/2019
2,12/11/2019
3 ,01/04/2019
];

MaintenancesTemp:
NoConcatenate load * inline [
Vehicle ,Date
1, 14/12/2018
1 ,16/04/2019
1 ,17/08/2019
1, 18/12/2019
2 ,01/01/2019
2, 01/06/2019
2 ,01/11/2019
2, 01/04/2020
3 ,01/02/2019
3, 01/05/2019
3 ,01/08/2019
3, 01/11/2019
3 ,01/02/2020
];

maintenance:

load Vehicle,
Date as StartDate,
if (isnull(Previous(Date)),today(),Previous(Date)) as Enddate
resident MaintenancesTemp
order by Vehicle,Date Desc;

drop Table MaintenancesTemp;

inner join IntervalMatch(Date,Vehicle) load StartDate,Enddate ,Vehicle Resident maintenance;

Vinerc
Contributor II
Contributor II
Author

Thanks for the reply Lironbaram,

I have just changed one line, from:

if (isnull(Previous(Date)),today(),Previous(Date)) as Enddate

To:

if (isnull(Previous(Date)) or Previous(Vehicle)<>Vehicle ,today(),Previous(Date)) as Enddate

Because the previous date was not null and it solved the problem.

Thanks for the help! I was having a hard time figuring out. Just one question, it created a synthetic key, is there any problem with that or I can leave it with that syn key?

 

Vinerc
Contributor II
Contributor II
Author

Just for the record, I solved the problem adding this piece of code:

data_new:
LOAD
Vehicle & Date as %ID,
Date
resident data;

Left Join

maintenance_new:
LOAD
Vehicle & Date as %ID,
StartDate
resident maintenance;

DROP Tables maintenance,data;


It's probably not the best way but I needed just one table to do some operations.