Find difference between two fields of two different values

I want to know how to get it done, and I just can't figure it out.

Let's say I have 3 logs.
Each log has fields 'buildNumber', 'startedAt', 'finishedAt' (and more unrelevant fields).

I want to calculate the:
difference between the last log 'finishedAt' with 'buildNumber' X to the first log 'startedAt' with 'buildNumber' X.

This way I want to get the runtime of the whole job with buildNumber X.

Thanks !!