I have got a large table with thousands of rows, but the basic pattern like:
Client | InceptionDate | ExpiryDate | KeyNo | Key2 | Type | Sequence |
AAA | 2006-01-01 | 2006-12-31 | 1 | 1 | 40 | 1 |
AAA | 2007-01-01 | 2007-12-31 | 2 | 1 | 20 | 2 |
BBB | 2005-01-01 | 2005-10-31 | 3 | 3 | 40 | 1 |
BBB | 2005-11-01 | 2006-05-31 | 4 | 3 | 20 | 2 |
BBB | 2006-06-01 | 2007-05-31 | 5 | 3 | 20 | 3 |
Can some T-SQL Guru help me to Write a script to find the latest Expirydate for each Client, when it is < Today, then Insert a new row under it with the same fields (except the Type must be 99, Sequence + 1), so the end result will look like this:
Client | InceptionDate | ExpiryDate | KeyNo | Key2 | Type | Sequence |
AAA | 2006-01-01 | 2006-12-31 | 1 | 1 | 40 | 1 |
AAA | 2007-01-01 | 2007-12-31 | 2 | 1 | 20 | 2 |
AAA | 2007-01-01 | 2007-12-31 | 2 | 1 | 99 | 3 |
BBB | 2005-01-01 | 2005-10-31 | 3 | 3 | 40 | 1 |
BBB | 2005-11-01 | 2006-05-31 | 4 | 3 | 20 | 2 |
BBB | 2006-06-01 | 2007-05-31 | 5 | 3 | 20 | 3 |
BBB | 2006-06-01 | 2007-05-31 | 5 | 3 | 99 | 4 |
Thanks a lot