Home > Net >  how to get start_date and end_date from creation_time
how to get start_date and end_date from creation_time

Time:09-01

need help, i need to get start_date and end_date from creation_time based on is_active state. I've tried several queries but didn't get the right result.

table example

id user_id name leader_name is_active creation_time
6 29 DF AS 0 2021-10-10
620 29 DF RB 0 2022-02-09
1088 29 DF AS 1 2022-06-30

And the result should look like this:

id user_id name leader_name is_active start_date end_date creation_time
6 29 DF AS 0 2021-10-10 2022-02-09 2021-10-10
620 29 DF RB 0 2022-02-09 2022-06-30 2022-02-09
1088 29 DF AS 1 2022-06-30 CURRENT_DATE() 2022-06-30

Please help my friends, thank you in advance

CodePudding user response:

Based on the information in the question section and comment section, I believe the row with is_active=1 has the latest creation_time for a group (based on user_id).Here is the query written and tested in workbench.

select id,user_id,name,leader_name,is_active,
t1.creation_time as start_date, case is_active when 0 then t2.creation_time else current_date() end as end_date,t1.creation_time
from (select id,user_id,name,leader_name,is_active,creation_time,@row_id:=@row_id 1 as row_id
    from test,(select @row_id:=0)t
    where user_id=29
    order by creation_time
    )t1
left join
    (select creation_time,@row_num:=@row_num 1 as row_num
    from test,(select @row_num:=0)t
    where user_id=29
    order by creation_time
    )t2
on t1.row_id 1=t2.row_num
;

-- result set:
# id, user_id, name, leader_name, is_active, start_date, end_date, creation_time
6, 29, DF, AS, 0, 2021-10-10, 2022-02-09, 2021-10-10
620, 29, DF, RB, 0, 2022-02-09, 2022-06-30, 2022-02-09
1088, 29, DF, AS, 1, 2022-06-30, 2022-08-31, 2022-06-30

That's not the end of it. Just in case you want to display the output based on each user_id group, here is the code:

-- first of all insert the following 4 lines on top of the original table data, which has the same user_id 50 
61  50  DF  AS  0   2021-10-10
630 50  DF  RB  0   2022-02-09
1188    50  DF  TS  0   2022-06-30
2288    50  DF  AS  1   2022-07-30

select  id,t1.user_id,name,leader_name,is_active,
t1.creation_time as start_date, case is_active when 0 then t2.creation_time else current_date() end as end_date,t1.creation_time
from 
 (select id,user_id,name,leader_name,is_active,creation_time,@row_id:=@row_id 1 as row_id
    from test,(select @row_id:=0)t
    order by user_id,creation_time
    )t1
left join
    (select user_id,creation_time,@row_num:=@row_num 1 as row_num
    from test,(select @row_num:=0)t
    order by user_id,creation_time
    )t2
on t1.user_id=t2.user_id and t1.row_id 1=t2.row_num
;

-- result set:
# id, user_id, name, leader_name, is_active, start_date, end_date, creation_time
6, 29, DF, AS, 0, 2021-10-10, 2022-02-09, 2021-10-10
620, 29, DF, RB, 0, 2022-02-09, 2022-06-30, 2022-02-09
1088, 29, DF, AS, 1, 2022-06-30, 2022-08-31, 2022-06-30
61, 50, DF, AS, 0, 2021-10-10, 2022-02-09, 2021-10-10
630, 50, DF, RB, 0, 2022-02-09, 2022-06-30, 2022-02-09
1188, 50, DF, TS, 0, 2022-06-30, 2022-07-30, 2022-06-30
2288, 50, DF, AS, 1, 2022-07-30, 2022-08-31, 2022-07-30


  • Related