I could use some help with a query to help me identify data missing from a table. In my example, I have a history table missing the initial entry, usually created by a trigger from another table, but the trigger was turned off. This table - we will call Table B - contains a DateTimeChanged and a DateTimeCreated column. The DateTimeChanged and DateTimeCreated columns should be equal on the initial entry to the table. However, since this trigger was off, I am missing an entry for a specific period.
I aim to identify instances where the earliest DateTimeChanged for each group of records does not match the DateTimeCreated value. The earliest DateTimeChanged for each record should always be equal to the DateTimeCreated. I can better determine my next steps by identifying where this is not true for my current data set.
Here is an example of the data:
Table A (Live)
| ID | Company | Created | Changed |
|---|---|---|---|
| 123456 | ABC123 | 2021-11-10 14:09:44.920 | 2022-05-19 11:13:17.137 |
Table B (History)
| ID | Company | Created | Changed |
|---|---|---|---|
| 123456 | ABC123 | 2021-11-10 14:09:44.920 | 2022-05-06 10:08:09.263 |
| 123456 | ABC123 | 2021-11-10 14:09:44.920 | 2022-05-19 11:13:17.137 |
In Table B, there is a missing row where the Created and Changed date should be the same. This is because a new line should be created in Table B upon any insert, update, or deletion from Table A, but the trigger was disabled.
How can I find all instances where the earliest record in Table B does not have a row with a DateTimeChanged equal to the DateTimeCreated in Table A for my ID and Company combination? I believe I would have to use the min(DateTimeChanged) in Table B to compare against the DateTimeCreated in Table A. But I have yet to find an effective way to do this for each ID and Company combination I have.
Thank you for your help!