一、目的
ClickHouse指标表中有个字段week_of_month,含义是这条数据属于本月第几周。
而且将本月第一天所在的那一周视为第一周,无论它是从周几开始的。比如2024-12-01是周日,即12月第一周。而2024-12-02是周一,即12月第二周
二、文心一言
文心一言的代码有点问题
三、SQL语句
selectday,toDate(concat(toString(toYear(day)), '-', lpad(toString(toMonth(day)), 2, '0'), '-01')) as first_day_of_month ,DAYOFMONTH(day) - 1 AS days_since_first_day,toDayOfWeek(first_day_of_month) AS first_day_of_week,(7 - (first_day_of_week - 1) % 7) AS days_in_first_week,CASEWHEN days_since_first_day < days_in_first_week THEN 1WHEN days_since_first_day >= days_in_first_week and days_since_first_day < days_in_first_week+7 THEN 2WHEN days_since_first_day >= days_in_first_week and days_since_first_day < days_in_first_week+14 THEN 3WHEN days_since_first_day >= days_in_first_week and days_since_first_day < days_in_first_week+21 THEN 4WHEN days_since_first_day >= days_in_first_week and days_since_first_day < days_in_first_week+28 THEN 5WHEN days_since_first_day >= days_in_first_week and days_since_first_day < days_in_first_week+35 THEN 6END AS week_of_month from hurys_jw.tb_week_of_month group by day ;
first_day_of_month
计算本月的第一天。days_since_first_day
计算从本月第一天到目标日期之间的天数差。first_day_of_week
获取本月第一天是周几。days_to_first_monday
计算从本月第一天到第一个周一之间的天数(如果第一天不是周一)
四、验证查询结果
和11月日历一一验证,没问题,搞定!