如何知道WEEK函数所涵盖的日期间隔



我想做另外两列,分别显示一周中分组的第一天和最后一天。

目前,我正在按周周期汇总一个查询。当我运行这个查询时,它会显示表上的结果:

SELECT 
pc.date,
CONCAT(YEAR(pc.date), '/', WEEK(pc.date)) as year_week  
FROM pc
GROUP BY CONCAT(YEAR(pc.date), '/', WEEK(pc.date))
ORDER BY pc.date
dateyear_week
2020-09-022020/35
2020-09-072020/36
2020-09-172020/37
2020-09-232020/38
2020-09-282020/39
2020-102020/40
2020-10-112020/41
2020-10-212020/42
2020-10-282020/43

您可以使用WEEKDAY函数。演示:

select 
date_add(dt, interval -WEEKDAY(dt)-1 day ) FirstDayOfWeek, 
date_add(date_add(dt, interval  -WEEKDAY(dt)-1 day), interval 6 day) LastDayOfWeek,
week(dt) wk
from (
select '2020-09-02' dt union all
select '2020-09-07' union all
select '2020-09-17' 
) t

返回

FirstDayOfWeek  LastDayOfWeek   wk
2020-08-30  2020-09-05  35
2020-09-06  2020-09-12  36
2020-09-13  2020-09-19  37

相关内容

  • 没有找到相关文章

最新更新