简体   繁体   English

如何从 creation_time 获取 start_date 和 end_date

[英]how to get start_date and end_date from creation_time

need help, i need to get start_date and end_date from creation_time based on is_active state.需要帮助,我需要根据 is_active state 从 creation_time 获取 start_date 和 end_date。 I've tried several queries but didn't get the right result.我尝试了几个查询,但没有得到正确的结果。

table example表格示例

id ID user_id用户身份 name姓名 leader_name领导者姓名 is_active活跃 creation_time创建时间
6 6 29 29 DF东风 AS作为 0 0 2021-10-10 2021-10-10
620 620 29 29 DF东风 RB RB 0 0 2022-02-09 2022-02-09
1088 1088 29 29 DF东风 AS作为 1 1 2022-06-30 2022-06-30

And the result should look like this:结果应该是这样的:

id ID user_id用户身份 name姓名 leader_name领导者姓名 is_active活跃 start_date开始日期 end_date结束日期 creation_time创建时间
6 6 29 29 DF东风 AS作为 0 0 2021-10-10 2021-10-10 2022-02-09 2022-02-09 2021-10-10 2021-10-10
620 620 29 29 DF东风 RB RB 0 0 2022-02-09 2022-02-09 2022-06-30 2022-06-30 2022-02-09 2022-02-09
1088 1088 29 29 DF东风 AS作为 1 1 2022-06-30 2022-06-30 CURRENT_DATE()当前的日期() 2022-06-30 2022-06-30

Please help my friends, thank you in advance请各位朋友帮忙,先谢谢了

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.根据问题部分和评论部分的信息,我相信 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

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:以防万一您想根据每个 user_id 组显示 output,下面是代码:

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


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL - 将日期列转换为开始和结束日期 - SQL - convert date column to start and end dates Bigquery 合并行,其中一行的开始日期是另一行的结束日期 - Bigquery merge row where start date for one row is the end date for another 如何使用 bigquery 从本月至今的所有卖家中获得前 1000 名卖家的交易金额百分比 - how to get % of transaction amount from top 1000 sellers over all sellers, Month to Date, using bigquery 如何在 Sql 中从特定日期到日期生成第 1 天、第 2 天、第 3 天等日期 - How generate days like day 1, day 2,day-3 from particular date to date in Sql 如何从给定日期开始将 BigQuery 数组的元素与日期匹配? - How to match elements of a BigQuery array with dates by starting from a given date? AWS Route 53 按创建日期列出记录 - AWS Route 53 list records by creation date 如何将 Firestore 日期/时间戳转换为 Kotlin 中的日期? - How do I convert a Firestore date/Timestamp to Date in Kotlin? 尝试从 Firestore 时间戳创建人类可读的日期/时间 - trying to create a human readable date / time from a firestore timestamp 有没有其他方法可以从日期中提取年份? - Is there a different way to extract the year from a date? 如何将特定日期创建的所有文件从一个存储桶复制到 GCS 中的另一个存储桶? - How to copy all the files created on a specific date from one bucket to another in GCS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM