Sample Header Ad - 728x90

Create SCD type 2 table from change log table using SQL

1 vote
0 answers
320 views
I have a change log table, which captures the transition of a state with its time, for example: user_id,user_status_from,user_status_to, created_date 7cc4d A2 A1 2019-11-03 23:04:26 7cc4d A1 A6 2019-11-03 23:05:28 7cc4d A6 I4 2019-11-16 10:00:34 7cc4d I4 A1 2020-03-16 10:00:36 Basically this table records the transition from one status to another, here it says that transition from **A2--> A1 occurred at 2019-11-03 23:04:26** (1st row) and then it moved from **A1 --> A6 at 2019-11-03 23:05:28** and so on. Now I want to write a SQL through which I should be able to create table in such a way that tells the history of any status, for example: in particular status user was from this date to that date, basically a life of any status, I want to create a table like below: userId status statrtingdatetime endingdatetime 7cc4d A1 2019-11-03 23:04:26 2019-11-03 23:05:28 7cc4d A6 2019-11-03 23:05:28 2019-11-16 10:00:34 7cc4d I4 2019-11-16 10:00:34 2020-03-16 10:00:36 7cc4d A1 2020-03-16 10:00:36 2020-04-02 08:24:50 If you see this table, it is nothing but some sort of logical table from above table, it says that a particular status life was from one date to another. How can write SQL to create this final table.(please note that **LAG/LEAD** function doesn't work in my version of MySql Server)
Asked by user3065757 (111 rep)
Apr 5, 2020, 04:02 AM