计算闰年的月份天数

Calculate Days in Month For Leap Year

对于另一个与日期相关的问题,我很抱歉,但我找不到解决此问题的 post。 我正在尝试向我的日期 table 添加一列,显示每个月的天数,并处理闰年 - 下面是我的进度:

case when
Date like '01%' then '31'
Date like '02%' and  then ''
Date like '03%' then '31'
Date like '04%' then '30'
Date like '05%' then '31'
Date like '06%' then '30'
Date like '07%' then '31'
Date like '08%' then '31'
Date like '09%' then '30'
Date like '10%' then '31'
Date like '11%' then '30'
Date like '12%' then '31'

end as DAYS_IN_MONTH

如果你有约会对象,那么你可以使用:

select date_part(day, last_day(datecol))

-- 生成带有日期的系列

with base_table as 
(
select
  dateadd(day, '-' || seq4(), current_date()) as dte
from
  table
    (generator(rowcount => 1095))
  )

-- 分组后统计一个月的天数

SELECT count(*) as days_in_month,
         date_trunc('Month',dte) as month
  FROM base_table
  -- Filter here for necessary days
  where dte between '2019-01-01' and '2019-12-31'
  GROUP BY 2

-- 加入这个 table 和你的 table