Heim  >  Fragen und Antworten  >  Hauptteil

Rufen Sie das Startdatum und das Enddatum der Erstellungszeit ab

Benötige Hilfe, ich muss Startdatum und Enddatum aus der Erstellungszeit basierend auf dem Status „is_active“ abrufen. Ich habe mehrere Abfragen versucht, aber nicht die richtigen Ergebnisse erhalten.

Formularbeispiel

id Benutzer-ID Name Name des Leiters ist_aktiv Erstellungszeit
6 29 DF AS 0 2021-10-10
620 29 DF RB 0 09.02.2022
1088 29 DF AS 1 30.06.2022

Das Ergebnis sollte so aussehen:

id Benutzer-ID Name Name des Leiters ist_aktiv Startdatum Enddatum Erstellungszeit
6 29 DF AS 0 2021-10-10 09.02.2022 2021-10-10
620 29 DF RB 0 09.02.2022 30.06.2022 09.02.2022
1088 29 DF AS 1 30.06.2022 CURRENT_DATE() 30.06.2022

Bitte helft meinen Freunden, vielen Dank im Voraus

P粉265724930P粉265724930240 Tage vor392

Antworte allen(1)Ich werde antworten

  • 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

    Antwort
    0
  • StornierenAntwort