案例陈述:WHEN 日期距当前日期超过 90 年 THEN 最大日期距当前日期 90 年

发布于 2025-01-18 05:54:10 字数 217 浏览 0 评论 0原文

我有一个包含人们生日的日期列。其中一些包含 90 多年前的日期,我希望这些日期的上限为 90 年前。

示例

current      desired
1930-01-01   1932-01-01
1930-02-11   1932-02-11

正如您所看到的,如果出生日期自然超过 90 年前,则期望的状态是使出生日期距当前日期 90 年。

I have a date column that contains people's birthdays. Some of them contain dates that are over 90 years ago and I want those to be capped at 90 years ago.

Example

current      desired
1930-01-01   1932-01-01
1930-02-11   1932-02-11

As you can see the desired state is to have the birthdate be 90 years from current date if it is naturally over 90 years ago.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猥琐帝 2025-01-25 05:54:10

这比我最初猜到的要棘手,而且可以更简单地写下,因为似乎有“竞争”,尽管它是易于阅读的,而是“

90年以上的无日期:

SELECT column1 as "current"
    ,iff(column1 < dateadd('year',-90, CURRENT_DATE), 
         date_from_parts(
            year(CURRENT_DATE) - 90 + (month(column1)*100+day(column1) < month(CURRENT_DATE)*100 + day(CURRENT_DATE))::int, 
             month(column1), 
             day(column1)), 
         column1
        ) as desired
FROM VALUES 
    ('1934-01-01'::date),
    ('1932-03-28'::date),
    ('1932-03-20'::date),
    ('1931-03-28'::date),
    ('1931-03-20'::date),
    ('1930-01-01'::date),
    ('1930-02-11'::date);
当前想要的
1934-01-- 011934-01-01
1932-03-281932-03-28
1932-03-201933-03-03-20
1931-03-281932-03-28 1931-28
1931-03-20 1933-201933-03-03-20
1930-20 1930-01--01- 011933-01-01
1930-02-111933-02-11

在90年前(又名标题)之前的一年(标题)版本:

SELECT column1 as "current"
    ,iff(year(column1) < year(CURRENT_DATE)-90, date_from_parts(year(CURRENT_DATE)-90, month(column1), day(column1)), column1) as desired
FROM VALUES
    ('1934-01-01'::date),
    ('1932-03-28'::date),
    ('1932-03-20'::date),
    ('1931-03-28'::date),
    ('1931-03-20'::date),
    ('1930-01-01'::date),
    ('1930-02-11'::date)
;
当前想要的
1934-01-01-01-011934-01-01-01-01-01
1932-03-281932 -03-28
1932-03-201932-03-20
1931-03-281932-03-28
1931-03-201932-03-03-20
1930-01-01-01-01 1932-01-01-01-01-011930-02-11
1932-11 1932-111932-11 -02-11

唯一的案例:

因此,正如案例语句的背景下所要求的那样,我再次写了它(并修复了一年的部分),但也将其解构为解构,因此可以看到逻辑:

SELECT column1 as birth_day
    ,dateadd('year',-90, CURRENT_DATE) as ninety_years_ago
    ,year(ninety_years_ago) as ninety_years_ago_year
    ,(month(CURRENT_DATE)*100) + day(CURRENT_DATE) as current_partial_year
    ,(month(birth_day)*100) + day(birth_day) as birth_day_partial_year
    ,case when birth_day_partial_year < current_partial_year then 1 else 0 end as partial_year_correction
    ,(birth_day_partial_year < current_partial_year)::int as current_partial_year_alt_version /* not used just showing how it works */
    ,case
        WHEN column1 < ninety_years_ago then
            date_from_parts(ninety_years_ago_year + partial_year_correction, month(birth_day), day(birth_day))
        ELSE 
            birth_day
        END as desired
FROM VALUES 
    ('1934-01-01'::date),
    ('1932-04-28'::date),
    ('1932-03-20'::date),
    ('1931-04-28'::date),
    ('1931-03-20'::date),
    ('1930-01-01'::date),
    ('1930-02-11'::date);

给予:给予:

berth_dayninety_years_agoninety_years_ago_ogo_yearcurrent_partial_year04-28birth_day_day_year_year_year_year_year_correctioncurruct1932-03-
​201932-03-311932331320111933-03-20
1931-04-281932-03-03-311932331428001932-04-28
1931-031-03-03-200011933-03-20
1930-01-011932-03-3119323311011 111933-01-01
1930-02-111932-03-03-311932 331331 331211111933-02-111 1933-02-11

it's more tricky than I initially guessed, and it can be written more simpler, now that there seems "be competition", albeit, this is less readable, than it was"

The no date over 90 years version:

SELECT column1 as "current"
    ,iff(column1 < dateadd('year',-90, CURRENT_DATE), 
         date_from_parts(
            year(CURRENT_DATE) - 90 + (month(column1)*100+day(column1) < month(CURRENT_DATE)*100 + day(CURRENT_DATE))::int, 
             month(column1), 
             day(column1)), 
         column1
        ) as desired
FROM VALUES 
    ('1934-01-01'::date),
    ('1932-03-28'::date),
    ('1932-03-20'::date),
    ('1931-03-28'::date),
    ('1931-03-20'::date),
    ('1930-01-01'::date),
    ('1930-02-11'::date);
currentDESIRED
1934-01-011934-01-01
1932-03-281932-03-28
1932-03-201933-03-20
1931-03-281932-03-28
1931-03-201933-03-20
1930-01-011933-01-01
1930-02-111933-02-11

The no year prior to the 90 years ago (aka the title) version:

SELECT column1 as "current"
    ,iff(year(column1) < year(CURRENT_DATE)-90, date_from_parts(year(CURRENT_DATE)-90, month(column1), day(column1)), column1) as desired
FROM VALUES
    ('1934-01-01'::date),
    ('1932-03-28'::date),
    ('1932-03-20'::date),
    ('1931-03-28'::date),
    ('1931-03-20'::date),
    ('1930-01-01'::date),
    ('1930-02-11'::date)
;
currentDESIRED
1934-01-011934-01-01
1932-03-281932-03-28
1932-03-201932-03-20
1931-03-281932-03-28
1931-03-201932-03-20
1930-01-011932-01-01
1930-02-111932-02-11

Only sing CASE:

So as it was asked for in the context of CASE statements, I have written it again (and fixed the year partials) but also left it deconstructed, so the logic can be seen:

SELECT column1 as birth_day
    ,dateadd('year',-90, CURRENT_DATE) as ninety_years_ago
    ,year(ninety_years_ago) as ninety_years_ago_year
    ,(month(CURRENT_DATE)*100) + day(CURRENT_DATE) as current_partial_year
    ,(month(birth_day)*100) + day(birth_day) as birth_day_partial_year
    ,case when birth_day_partial_year < current_partial_year then 1 else 0 end as partial_year_correction
    ,(birth_day_partial_year < current_partial_year)::int as current_partial_year_alt_version /* not used just showing how it works */
    ,case
        WHEN column1 < ninety_years_ago then
            date_from_parts(ninety_years_ago_year + partial_year_correction, month(birth_day), day(birth_day))
        ELSE 
            birth_day
        END as desired
FROM VALUES 
    ('1934-01-01'::date),
    ('1932-04-28'::date),
    ('1932-03-20'::date),
    ('1931-04-28'::date),
    ('1931-03-20'::date),
    ('1930-01-01'::date),
    ('1930-02-11'::date);

gives:

BIRTH_DAYNINETY_YEARS_AGONINETY_YEARS_AGO_YEARCURRENT_PARTIAL_YEARBIRTH_DAY_PARTIAL_YEARPARTIAL_YEAR_CORRECTIONCURRENT_PARTIAL_YEAR_ALT_VERSIONDESIRED
1934-01-011932-03-311932331101111934-01-01
1932-04-281932-03-311932331428001932-04-28
1932-03-201932-03-311932331320111933-03-20
1931-04-281932-03-311932331428001932-04-28
1931-03-201932-03-311932331320111933-03-20
1930-01-011932-03-311932331101111933-01-01
1930-02-111932-03-311932331211111933-02-11
z祗昰~ 2025-01-25 05:54:10

肯定很棘手。我什至在timeanddate.com上检查了最大90年的日期差异 - 看起来还不错。如果有人在此发现问题,请感谢反馈。

select iff(extract(year,dateadd('year',90,column1::date))>extract(year,'2022-02-28'::date),
    column1,
    dateadd(day,
    datediff(day,column1::date,dateadd(year,-90,'2022-02-28'::date)),
    column1::date
    )) as test
    from
    values
    ('1932-02-29'::date)
    ,('1932-02-28'::date)
    ,('1932-03-01'::date)
    ,('1928-02-29'::date)
    ,('1924-02-29'::date)
    ,('1930-01-01'::date)
    ,('1940-05-01'::date)
    ,('1950-12-01'::date)
    ,('1980-10-01'::date)
    ,('1920-06-01'::date)
    ;
    
+------------+
| TEST       |
|------------|
| 1932-02-28 |
| 1932-02-28 |
| 1932-02-28 |
| 1932-02-28 |
| 1932-02-28 |
| 1932-02-28 |
| 1940-05-01 |
| 1950-12-01 |
| 1980-10-01 |
| 1932-02-28 |
+------------+

It was sure tricky one. I even checked date diff for 90 years max at timeanddate.com - looks ok. Do appreciate feedback if anyone finds issue in this.

select iff(extract(year,dateadd('year',90,column1::date))>extract(year,'2022-02-28'::date),
    column1,
    dateadd(day,
    datediff(day,column1::date,dateadd(year,-90,'2022-02-28'::date)),
    column1::date
    )) as test
    from
    values
    ('1932-02-29'::date)
    ,('1932-02-28'::date)
    ,('1932-03-01'::date)
    ,('1928-02-29'::date)
    ,('1924-02-29'::date)
    ,('1930-01-01'::date)
    ,('1940-05-01'::date)
    ,('1950-12-01'::date)
    ,('1980-10-01'::date)
    ,('1920-06-01'::date)
    ;
    
+------------+
| TEST       |
|------------|
| 1932-02-28 |
| 1932-02-28 |
| 1932-02-28 |
| 1932-02-28 |
| 1932-02-28 |
| 1932-02-28 |
| 1940-05-01 |
| 1950-12-01 |
| 1980-10-01 |
| 1932-02-28 |
+------------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文