I am trying to make SELECT statement for following situation and need help to make this SELECT statement. It's SQL Server 2005.
When the select statement is run, it should return rows which have SentDate as NULL assuming that there are no duplicate PersonID in table. It will return result set with Status as 'Initial Record' (as the same person id was not sent already). Once data is exported, SentDate will have today's date.
Now assume that two new row have been added and PersonID is duplicate. So when statement is run, it should return only newly added two records but status as 'Follow up Record' because same Person was exported in previous export job.
Here is the example of what is expected:
The initial table state would be:
RecordId PersonId SentDate CreatedDate
1 1 NULL 8/8/2010
2 2 NULL 8/8/2010
3 3 NULL 8/8/2010
When first time run, it should return following resultset:
RecordId PersonId RecordStatus
1 1 'Initial Record'
2 2 'Initial Record'
3 3 'Initial Record'
Now the state of the table will be:
RecordId PersonId SentDate CreatedDate
1 1 8/9/2010 8/8/2010
2 2 8/9/2010 8/8/2010
3 3 8/9/2010 8/8/2010
Suppose later a new has been added in table and so table values will be:
RecordId PersonId SentDate CreatedDate
1 1 8/9/2010 8/8/2010
2 2 8/9/2010 8/8/2010
3 3 8/9/2010 8/8/2010
4 2 NULL 8/8/2010
5 2 NULL 8/8/2010
Now this query should return
RecordId PersonId RecordStatus
4 2 'Follow up Record'
5 2 'Follow up Record'