搜尋

首頁  >  問答  >  主體

取得創建時間的開始日期和結束日期

需要協助,我需要根據is_active狀態從creation_time取得start_date和end_date。 我嘗試了幾次查詢,但沒有得到正確的結果。

表格範例

id user_id 姓名 leader_name is_active 建立時間
6 29 DF AS 0 2021-10-10
620 29 DF RB 0 2022-02-09
1088 29 DF AS 1 2022-06-30

結果應該如下:

id user_id 姓名 leader_name is_active 開始日期 結束日期 建立時間
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

請幫我的朋友,先謝謝你們

P粉265724930P粉265724930308 天前473

全部回覆(1)我來回復

  • P粉495955986

    P粉4959559862024-02-22 17:28:02

    根據問題部分和評論部分中的信息,我相信 is_active=1 的行具有群組的最新創建時間(基於 user_id)。這是在工作台中編寫和測試的查詢。

    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

    事情還沒結束。如果您想要根據每個 user_id 群組顯示輸出,請使用以下程式碼:

    -- 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

    回覆
    0
  • 取消回覆