Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help creating complex T-SQL SELECT statement
    text
    copied!<p>I am trying to make SELECT statement for following situation and need help to make this SELECT statement. It's SQL Server 2005.</p> <p>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. </p> <p>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.</p> <p>Here is the example of what is expected:</p> <p>The initial table state would be:</p> <p>RecordId&nbsp;&nbsp;PersonId&nbsp;&nbsp;SentDate&nbsp;&nbsp;CreatedDate<br/> 1&nbsp;&nbsp;1&nbsp;&nbsp;NULL&nbsp;&nbsp;8/8/2010<br/> 2&nbsp;&nbsp;2&nbsp;&nbsp;NULL&nbsp;&nbsp;8/8/2010<br/> 3&nbsp;&nbsp;3&nbsp;&nbsp;NULL&nbsp;&nbsp;8/8/2010<br/></p> <p>When first time run, it should return following resultset:<br/></p> <p>RecordId&nbsp;&nbsp;PersonId&nbsp;&nbsp;RecordStatus<br/> 1&nbsp;&nbsp;1&nbsp;&nbsp;'Initial Record'<br/> 2&nbsp;&nbsp;2&nbsp;&nbsp;'Initial Record'<br/> 3&nbsp;&nbsp;3&nbsp;&nbsp;'Initial Record'<br/></p> <p>Now the state of the table will be:<br/></p> <p>RecordId&nbsp;&nbsp;PersonId&nbsp;&nbsp;SentDate&nbsp;&nbsp;CreatedDate<br/> 1&nbsp;&nbsp;1&nbsp;&nbsp;8/9/2010&nbsp;&nbsp;8/8/2010<br/> 2&nbsp;&nbsp;2&nbsp;&nbsp;8/9/2010&nbsp;&nbsp;8/8/2010<br/> 3&nbsp;&nbsp;3&nbsp;&nbsp;8/9/2010&nbsp;&nbsp;8/8/2010<br/></p> <p>Suppose later a new has been added in table and so table values will be:</p> <p>RecordId&nbsp;&nbsp;PersonId&nbsp;&nbsp;SentDate&nbsp;&nbsp;CreatedDate<br/> 1&nbsp;&nbsp;1&nbsp;&nbsp;8/9/2010&nbsp;&nbsp;8/8/2010<br/> 2&nbsp;&nbsp;2&nbsp;&nbsp;8/9/2010&nbsp;&nbsp;8/8/2010<br/> 3&nbsp;&nbsp;3&nbsp;&nbsp;8/9/2010&nbsp;&nbsp;8/8/2010<br/> 4&nbsp;&nbsp;2&nbsp;&nbsp;NULL&nbsp;&nbsp;8/8/2010<br/> 5&nbsp;&nbsp;2&nbsp;&nbsp;NULL&nbsp;&nbsp;8/8/2010<br/></p> <p>Now this query should return </p> <p>RecordId&nbsp;&nbsp;PersonId&nbsp;&nbsp;RecordStatus<br/> 4&nbsp;&nbsp;2&nbsp;&nbsp;'Follow up Record'<br/> 5&nbsp;&nbsp;2&nbsp;&nbsp;'Follow up Record'<br/></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload