mysql 按周统计语句(周日为星期第一天,周六为最后一天)

select DATE_FORMAT(table.dateTimeColumns,'%Y-%U') weeks,count(distinct table.dateTimeColumns) countNum,
case  when DAYNAME(DATE_FORMAT(table.dateTimeColumns,'%Y-%m-%d'))='Sunday' then DATE_FORMAT(table.dateTimeColumns,'%Y-%m-%d')
     else date_add(DATE_FORMAT(table.dateTimeColumns,'%Y-%m-%d'),interval-DAYOFWEEK(DATE_FORMAT(table.dateTimeColumns,'%Y-%m-%d'))+1 day) end firstDay,
case when DAYNAME(DATE_FORMAT(table.dateTimeColumns,'%Y-%m-%d'))='Saturday' THEN DATE_FORMAT(table.dateTimeColumns,'%Y-%m-%d')
else date_add(DATE_FORMAT(table.dateTimeColumns,'%Y-%m-%d'),interval 7-DAYOFWEEK(table.dateTimeColumns)+0 day) end lastDay
from table
where table.dateTimeColumns between '2018-01-04 00:00:00' and '2018-02-03 23:59:59' group by weeks order by weeks

你可能感兴趣的:(mysql 按周统计语句(周日为星期第一天,周六为最后一天))