0

我正在尝试从 databricks sql 中的日期获取 week_of_the_month

这是我的 SQL:

with date as (
  select EXTRACT(DAY FROM '2017-01-01') as day
)

select case
       when day < 8 then '1'
       when day < 15 then '2'
       when day < 22 then '3'
       else '4'
       end as week_of_month

上面的 sql 在某些边缘情况下会失败。例如2010-03-31,那么week_of_month = 5

如何在 databricks SQL 中找到 week_of_month?

4

1 回答 1

1

按照你的说法,你会添加另一个when子句

select case
         when day < 8 then '1'
         when day < 15 then '2'
         when day < 22 then '3'
         when day < 29 then '4'   --> new
         else '5'                 --> modified
       end as week_of_month
于 2022-01-05T20:54:44.480 回答