I am new to SQL and trying to achieve something.
I have a table having fields like:
CustId Fname Lname Address1 Address2 Zip State LoadDate Flag 1 John Leo xxx xxx 34532 VA 1/1/2012 Y 2 Mike Saunder xxx xxx 94090 CA 1/1/2012 Y
Everytime I insert a new record it will set its flag to Y but lets say if any of the record value changes then new one will be set to Flag Y and old one to N. So for example if Mikes address1 is changed.
CustId Fname Lname Address1 Address2 Zip State LoadDate Flag 1 John Leo xxx xxx 34532 VA 1/1/2012 Y 2 Mike Saunder xxx xxx 94090 CA 1/1/2012 N 3 Mike Saunder rrr xxx 94090 CA 2/1/2012 Y
Assuming that you’re trying to say that the Fname/Lname pair is a unique key that lets you match rows:
EDIT:
I still think this is not the direction you want to be heading. For example, it doesn’t gracefully accomodate more than one update per customer per day unless you rely on the CustId to be a tie breaker.
A marginally better solution would be to replace Flag with a PreviousId field that is NULL in the first row for a customer and in later rows contains the CustId of the row that is being superseded. That links together the changes for each customer and provides a well defined order in the event that a customer is updated repeatedly on a single date.