I believe you're looking for a window join. If you post the code to generate the above tables it's easier for others to validate
https://code.kx.com/q/ref/wj/