Monday, March 2, 2015

T-SQL Query - Find simple and complex record


My Table has Four columns:

key_Column, Start_date, End_date, Status.



I should update column "status" as simple or complex depending upon the 1st three columns.


Condition for updating status columns as simple:

For the same key value(1st column), if the 1st record end_date and 2nd record start_dt falls within 90 days difference. Then the status should be updated as simple.

Note: There could be more than 3records in this case.

1st record end_date should fall in 90 days difference with 2nd record start_date.

2nd record end_date should fall in 90 days difference with 3rd record start_date.. (ie, 3rd record start_dt-90<=2nd record end_date) and so on..


Condition for updating status columns as complex:


For the same key value(1st column), if the 1st record end_date and 2nd record start_dt have difference more than 90 days. Then the status should be updated as complex.

Note: There could be more than 3records in this case.

For example, lets say a key value K2 has three records as shown in the above figure,

if the 1st two records satisfy the condition of simple.

but the 2nd and 3rd record of K2 key value satisfy condition of Complex. Then all the three records status should be updated as complex..

ie. 1st priority should be complex ..



Please give a suitable query with good performance..

Thanks in advance.





No comments:

Post a Comment