Page 1 of 1

Update previous records with yesterday's date

Posted: Mon Jul 28, 2014 11:12 am
by sam334
All,
Need a help on updating previous records with yesterdays date.
We have table Employee detail, where we have
employeeid, department, start_date, end_date.
Sam, IT, 2014-07-25, 12/31/9999

Now, when the employee change the department, it should update the end_date to yesterdays date and insert start_date as today's date.
employeeid, department, start_date, end_date.
Sam, IT, 2014-07-25, 2014-07-27
Sam, IT Assurance,2014-07-28, 12/31/9999

Any lead how to achieve it in server job. adding start date as today's date and end date as today's date - 1 won't work as it will update the current record instead of previous record.

Thanks.

Re: Update previous records with yesterday's date

Posted: Mon Jul 28, 2014 12:20 pm
by bharathappriyan
Hi,

Sort the data based on start_dt desc. Assign yesterday's date as Start date and the High end date as end date for employee's first record. For the second record, use (Previous record's start date -1 ) as end date.

Thanks,
Bharathappriyan

Posted: Mon Jul 28, 2014 1:05 pm
by sam334
Thanks @bharathappriyan. which functions should I use, like

I need something that will update previous row's end date to yesterday's date (from default date) if the employee change the department and new record comes from source data.

Posted: Mon Jul 28, 2014 2:37 pm
by ray.wurlod
Today's date is given by Date() therefore yesterday's date is given by Date() - 1 since dates are stored internally as integer offsets from a known day zero.

Initialize two stage variables with these values so that they only need to be calculated once.

If needed, use Oconv() functions to convert to the required string format.

Posted: Mon Jul 28, 2014 2:49 pm
by chulett
So you're just asking how to do a Type 2 change? Meaning, a new record is inserted for the effective date of the change and the original / old record is closed / marked as no longer current in some fashion.

You must be checking for existence already so you know whether this is a new or changed record. When you don't get a hit on the lookup you are inserting a new record using the current date and that '12/31/9999' end date, correct? Well, you would always do that step regardless of the lookup success or failure. A change just adds one more link to the target: the update link where whatever "primary key" identifies the old row (that you captured from the lookup) is used to update the END_DATE field of that record to your "current date -1" value. Constrain that link to only fire if the lookup succeeds.

Note that this assumes a record is always either new or a change and that is why you are processing it. There is a little bit more to it if you may be getting unchanged data and need to check to see if anything changed.

Oh, and since a record can go through multiple changes, make sure your existence lookup only queries for the 'most current' version - i.e. in your case that looks like the record with the 12/31/9999 date.

Posted: Tue Jul 29, 2014 10:24 pm
by sam334
Thanks for such a valuable information Craig and Ray. I can actually create another job to update the rows whenever there is a change record. But lookup is always better. Will let you know if I have any issue. For now workaround.....Thanks again.

Posted: Wed Jul 30, 2014 7:45 am
by chulett
OK... just a couple of small points. There's absolutely no need for a second job. And I wouldn't consider anything I wrote up to be any kind of a 'workaround' but rather How It Is Done. FYI.

Posted: Wed Jul 30, 2014 8:50 am
by sam334
Craig, Let me understand the design.

Currently, the design is
ODBC------
FTP Stage- Tranformer- ODBC

Reference odbc is same as target odbc where it updates old records and insert new records.

you want me to add one more link to update the records of lookup succeeds correct?

Posted: Wed Jul 30, 2014 9:54 am
by chulett
Correct. Two links from the Transformer to the target, one for inserts and one for updates.

Posted: Wed Jul 30, 2014 9:39 pm
by sam334
Okay. Thanks Craig. It worked. Awesome..