获取此日期列并将它们分组 1-30、31-59、60-89、+90,包括即将到来的日期和已经过去的日期
Taking this column of dates and grouping them 1-30, 31- 59, 60 - 89, +90, both upcoming and those dates that have passes
enter image description here我有一个日期列,我试图将其分组到特定的组中。一些日期已经过去,其他日期在未来。我想按 1-30、31-59、60-89、+90 对它们进行分组,因此已经通过的和即将通过的总共有 8 个组。以下是我到目前为止所写的内容,但我觉得我过于复杂了,我做的调整越多,它就变得越不正确。感谢任何见解!
CASE WHEN DATEDIFF(CURRENT_DATE(),`date`) >= 90 THEN '90 days past'
WHEN DATEDIFF(CURRENT_DATE(),`date`) >= 60 THEN '60 days past'
WHEN DATEDIFF(CURRENT_DATE(),`date`) >= 30 THEN '30 days past'
WHEN DATEDIFF(`date`,CURRENT_DATE()) >= 90 THEN '90 days future'
WHEN DATEDIFF(`date`,CURRENT_DATE()) >= 60 THEN '60 days future'
WHEN DATEDIFF(`date`,CURRENT_DATE()) >= 30 THEN 'Next 30 days'
ELSE 'Not Late'
END
我觉得还行
我会为每个使用相同的 datediff。
create table test (`date` date);
insert into test values
( date_add(current_date, interval -90 day) ),
( date_add(current_date, interval -89 day) ),
( date_add(current_date, interval -60 day) ),
( date_add(current_date, interval -59 day) ),
( date_add(current_date, interval -30 day) ),
( date_add(current_date, interval -29 day) ),
( date_add(current_date, interval 29 day) ),
( date_add(current_date, interval 30 day) ),
( date_add(current_date, interval 59 day) ),
( date_add(current_date, interval 60 day) ),
( date_add(current_date, interval 89 day) ),
( date_add(current_date, interval 90 day) )
select `date`,
CASE
WHEN DATEDIFF(`date`, CURRENT_DATE) <= -90 THEN '90 days past'
WHEN DATEDIFF(`date`, CURRENT_DATE) <= -60 THEN '60 days past'
WHEN DATEDIFF(`date`, CURRENT_DATE) <= -30 THEN '30 days past'
WHEN DATEDIFF(`date`, CURRENT_DATE) >= 90 THEN '90 days future'
WHEN DATEDIFF(`date`, CURRENT_DATE) >= 60 THEN '60 days future'
WHEN DATEDIFF(`date`, CURRENT_DATE) >= 30 THEN 'Next 30 days'
ELSE 'Not Late'
END as status
from test
date | status
:--------- | :-------------
2021-10-12 | 90 days past
2021-10-13 | 60 days past
2021-11-11 | 60 days past
2021-11-12 | 30 days past
2021-12-11 | 30 days past
2021-12-12 | Not Late
2022-02-08 | Not Late
2022-02-09 | Next 30 days
2022-03-10 | Next 30 days
2022-03-11 | 60 days future
2022-04-09 | 60 days future
2022-04-10 | 90 days future
db<>fiddle here
enter image description here
CASE WHEN DATEDIFF(CURRENT_DATE(),`date`) >= 90 THEN '90 days past'
WHEN DATEDIFF(CURRENT_DATE(),`date`) >= 60 THEN '60 days past'
WHEN DATEDIFF(CURRENT_DATE(),`date`) >= 30 THEN '30 days past'
WHEN DATEDIFF(`date`,CURRENT_DATE()) >= 90 THEN '90 days future'
WHEN DATEDIFF(`date`,CURRENT_DATE()) >= 60 THEN '60 days future'
WHEN DATEDIFF(`date`,CURRENT_DATE()) >= 30 THEN 'Next 30 days'
ELSE 'Not Late'
END
我觉得还行
我会为每个使用相同的 datediff。
create table test (`date` date); insert into test values ( date_add(current_date, interval -90 day) ), ( date_add(current_date, interval -89 day) ), ( date_add(current_date, interval -60 day) ), ( date_add(current_date, interval -59 day) ), ( date_add(current_date, interval -30 day) ), ( date_add(current_date, interval -29 day) ), ( date_add(current_date, interval 29 day) ), ( date_add(current_date, interval 30 day) ), ( date_add(current_date, interval 59 day) ), ( date_add(current_date, interval 60 day) ), ( date_add(current_date, interval 89 day) ), ( date_add(current_date, interval 90 day) )
select `date`, CASE WHEN DATEDIFF(`date`, CURRENT_DATE) <= -90 THEN '90 days past' WHEN DATEDIFF(`date`, CURRENT_DATE) <= -60 THEN '60 days past' WHEN DATEDIFF(`date`, CURRENT_DATE) <= -30 THEN '30 days past' WHEN DATEDIFF(`date`, CURRENT_DATE) >= 90 THEN '90 days future' WHEN DATEDIFF(`date`, CURRENT_DATE) >= 60 THEN '60 days future' WHEN DATEDIFF(`date`, CURRENT_DATE) >= 30 THEN 'Next 30 days' ELSE 'Not Late' END as status from test
date | status :--------- | :------------- 2021-10-12 | 90 days past 2021-10-13 | 60 days past 2021-11-11 | 60 days past 2021-11-12 | 30 days past 2021-12-11 | 30 days past 2021-12-12 | Not Late 2022-02-08 | Not Late 2022-02-09 | Next 30 days 2022-03-10 | Next 30 days 2022-03-11 | 60 days future 2022-04-09 | 60 days future 2022-04-10 | 90 days future
db<>fiddle here