I have one table (approx 800k rows), '**A**' which for the sake of this post has an Id and a StatusId. Another table '**B**', stores the duration (fromDate and toDate) of a row in **A** maintaining a single status - which is updated once a day.

```
A: id, statusId,

B: idFromA, fromDate, toDate, statusId

```

For instance if we have a row: (id: 123, statusId: 1) which holds onto that statusId for 2 days and then on the 3rd day changes to (id: 123, statusId: 2) then table **B** would be used to record this change.

I need to write the query for this but I'm not sure how you would compare the current statusId of each row in **A** to that of the latest row with the same id in **B** - and then depending on that either insert a new row or extend the toDate (by in this case a day) - any pointers would help