First I would like to say that I am new to the stackoverflow community and relatively new to SQL itself and so please pardon me If I didn't format my question right or didn't state my requirements clearly.
I am trying to implement a type 2 SCD in Oracle. The structure of the source table (customer_records
) is given below.
CREATE TABLE customer_records(
day date,
snapshot_day number,
vendor_id number,
customer_id number,
rank number
);
INSERT INTO customer_records
(day,snapshot_day,vendor_id,customer_id,rank)
VALUES
(9/24/2014,6266,71047795,476095,3103),
(10/1/2014,6273,71047795,476095,3103),
(10/8/2014,6280,71047795,476095,3103),
(10/15/2014,6287,71047795,476095,3103),
(10/22/2014,6291,71047795,476095,3102),
(10/29/2014,6330,71047795,476095,3102),
(11/05/2015,6351,71047795,476095,3102),
(11/12/2015,6440,71047795,476095,3103);
The above table is updated weekly and I have pulled records for a particular customer represented by vendor_id
and customer_id
. Such that each customer will have a unique vendor_id
and customer_id
. I am trying to track the changes in the tier (rank
) of a customer. It may so happen that the customer's tier may remain same for several weeks and we are only willing to track when there is a change in the tier of the customer.
The desired output (dimension table) would look something like this:
SK Version Date_From Date_To Vendor_id Customer_Id Rank_Id
1 1 9/24/2014 10/22/2014 71047795 476095 3103
2 2 10/22/2014 11/05/2015 71047795 476095 3102
3 3 11/05/2015 12/31/2199 71047795 476095 3103
Such that whenever customer's tier hit a change we track that in a new table. Also, wanting to include the current_flag
= 'Y' for the most current tier.
I want to be able to do it using merge.