且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将日期转换为ISO周日期

更新时间:2023-11-29 12:50:10

认为 Impala返回 date_part() extract()的等值周-根据您之前的问题。没有文档可以证明这种效果。

I think Impala returns the iso week for date_part() and extract() -- based on your previous question. There is no documentation to this effect.

如果是这样,则可以使用条件逻辑:

If so, you can use conditional logic:

select (case when date_part(week, datecol) = 1 and
                  date_part(year, datecol) <> date_part(year, datecol + interval 1 week)
             then date_part(year, datecol) + 1
             when date_part(week, datecol) in (52, 53) and
                  date_part(year, datecol) <> date_part(year, datecol - interval 1 week)
             then date_part(year, datecol) - 1
             else date_part(year, datecol)
         end) as iso_year,
        date_part(week, datecol) as iso_week

否则,您可以使用以下方法获得等年的第一天:

Otherwise, you can get the first day of the iso year using:

select (case when to_char('DD', date_trunc(year, datecol), 'DD') in ('THU', 'FRI', 'SAT', 'SUN')
             then next_day(date_trunc(year, date_trunc(year, datecol)), 'Monday')
             else next_day(date_trunc(year, date_trunc(year, datecol)), 'Monday') - interval 7 day
         end) as iso_year_start 

然后可以使用算术从等年开始时计算等周。

You can then calculate the iso week from the start of the iso year using arithmetic.