I’ve a change log desk, which captures the transition of a state with its time, for instance:
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
Principally this desk information the transition from one standing to a different, right here it says that transition from
A2–> A1 occurred at 2019-11-03 23:04:26 (1st row) after which it moved from
A1 –> A6 at 2019-11-03 23:05:28 and so forth.
Now I need to write a SQL by which I ought to be capable of create desk in such a manner that tells the historical past of any standing, for instance: specifically standing person was from this date to that date, principally a lifetime of any standing, I need to create a desk like beneath:
userId standing 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
In the event you see this desk, it’s nothing however some form of logical desk from above desk, it says {that a} specific standing life was from one date to a different.
How can write SQL to create this ultimate desk.(please observe that LAG/LEAD operate would not work in my model of MySql Server)