将日期转换为iso周日期

dfuffjeb  于 2021-06-26  发布在  Impala
关注(0)|答案(1)|浏览(398)

如何在impalasql中将日期转换为iso周日期?
例如 2019-12-30 在iso周中,日期日历将写为 2020-W01-1 或者 2020W011 回答:
标记戈登林诺夫的答案是正确的,因为它解决了问题的关键部分,年的iso周日期部分的推导。
对于iso周日期的周部分,有一个就绪函数,iso周日期的日部分可以轻松地从星期日开始的一周转换为星期一开始的一周。
以下查询包含从星期一到星期天的所有星期日期:

select datecol, 
       concat(cast(iso_year as string),'-W',lpad(cast(iso_week as string),2,'0'),'-',cast(iso_day as string)) as iso_Year_week_date_long,
       concat(cast(iso_year as string),'W',lpad(cast(iso_week as string),2,'0'),cast(iso_day as string)) as iso_Year_week_date_short
  from (
SELECT datecol, 
       (case when weekofyear(datecol) = 1 and
                  date_part('year',datecol) <> date_part('year',adddate(datecol,+7))
             then date_part('year',datecol) + 1
             when weekofyear(datecol) in (52, 53) and
                  date_part('year',datecol) <> date_part('year',adddate(datecol,-7))
             then date_part('year',datecol) - 1
             else date_part('year',datecol)
         end) as iso_year,
       weekofyear(datecol) as iso_week,
       1+mod(dayofweek(datecol)+5,7) as iso_day
  from (
  select '2021-12-31' as datecol union
  select '2020-12-31' as datecol union
  select '2019-12-31' as datecol union
  select '2018-12-31' as datecol union
  select '2017-12-31' as datecol union
  select '2016-12-31' as datecol union
  select '2015-12-31' as datecol union
  select '2014-12-31' as datecol union
  select '2013-12-31' as datecol union
  select '2012-12-31' as datecol union
  select '2022-01-01' as datecol union
  select '2021-01-01' as datecol union
  select '2020-01-01' as datecol union
  select '2019-01-01' as datecol union
  select '2018-01-01' as datecol union
  select '2017-01-01' as datecol union
  select '2016-01-01' as datecol union
  select '2015-01-01' as datecol union
  select '2014-01-01' as datecol union
  select '2013-01-01' as datecol
 ) as t1
  ) as t2
order by datecol;

显示了一月一号是属于谁的
新年,如果1月1日是一周中的第1、2、3或4天,即在包含1月1日的一周中至少有4个新年
旧年,如果1月1日是一周中的第5、6或7天,即在包含1月1日的一周中有3个或更少的新年

datecol   |iso_year_week_date_long|iso_year_week_date_short|
----------|-----------------------|------------------------|
2014-12-31|2015-W01-3             |2015W013                |
2015-01-01|2015-W01-4             |2015W014                |
2015-12-31|2015-W53-4             |2015W534                |
2016-01-01|2015-W53-5             |2015W535                |
2016-12-31|2016-W52-6             |2016W526                |
2017-01-01|2016-W52-7             |2016W527                |
2017-12-31|2017-W52-7             |2017W527                |
2018-01-01|2018-W01-1             |2018W011                |
2018-12-31|2019-W01-1             |2019W011                |
2019-01-01|2019-W01-2             |2019W012                |
2019-12-31|2020-W01-2             |2020W012                |
2020-01-01|2020-W01-3             |2020W013                |
2020-12-31|2020-W53-4             |2020W534                |
2021-01-01|2020-W53-5             |2020W535                |
8iwquhpp

8iwquhpp1#

我想 Impala 返回iso周 date_part() 以及 extract() --基于你之前的问题。没有这方面的文件。
如果是,可以使用条件逻辑:

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

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

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

然后可以使用算术计算从iso年开始的iso周。

相关问题