十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
不太明白你的意思。我想这样,按自然周来计算,一年共52周
网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了阿勒泰免费建站欢迎大家使用!
select DATEPART(YEAR,[时间字段])*100+DATEPART(MONTH,[时间字段]),datepart(week,[时间字段]),SUM([数量]) from 表名
group by DATEPART(YEAR,[时间字段])*100+DATEPART(MONTH,[时间字段]),datepart(week,[时间字段])
财年是一个公司的上市时间决定的,所以不同的公司财年码表不同,因为我目前用的财年是从4月1号开始算的,自己就写了个mysql 的工具函数:
SELECT
-- 自然日
naturalday,
-- 财年
concat('FY', DATE_FORMAT(naturalday,'%y'), DATE_FORMAT(naturalday,'%y')+1) as fiscalyear,
-- 财季
concat('Q', case when QUARTER(naturalday)=4 then 1 ELSE QUARTER(naturalday)-1 END
) as fiscalquarter,
-- 财月
DATE_FORMAT(naturalday,'%m') as fiscalmonth,
-- 财周
timestampdiff(WEEK,date(concat(year(naturalday),'-',elt(quarter(naturalday),1,4,7,10),'-',1)),naturalday) as fiscalweek
from tool_lenovo_day;
一下是我写的一个存储过程:
CREATE DEFINER=`root`@`%` PROCEDURE `sp_InitialFiscalYear_data`(`sDate` varchar(20),`eDate` varchar(20))
BEGIN
/*
工具函数:生成时间段内的lenovo的财年、财季、财月、财周数据
做成:白纸黑字
做成日:2020-07-16
入参:
sDate 开始自然日
eDate 结束自然日
业务逻辑:
tool_lenovo_day 财年表
有更新、没有新增财年数据
*/
INSERT INTO tool_lenovo_day SELECT
*
FROM
(
SELECT
naturalday,
concat( 'FY', DATE_FORMAT( naturalday, '%y' ), DATE_FORMAT( naturalday, '%y' )+ 1 ) AS fiscalyear,
concat( 'Q', CASE WHEN QUARTER ( naturalday )= 4 THEN 1 ELSE QUARTER ( naturalday )- 1 END ) AS fiscalquarter,
DATE_FORMAT( naturalday, '%m' ) AS fiscalmonth,
timestampdiff(
WEEK,
date(
concat( YEAR ( naturalday ), '-', elt( QUARTER ( naturalday ), 1, 4, 7, 10 ), '-', 1 )),
naturalday
)+1 AS fiscalweek
FROM
(
SELECT
date_add( date_sub(sDate,interval 1 day), INTERVAL ( cast( help_topic_id AS signed INTEGER ) + 1 ) day ) naturalday
FROM
mysql.help_topic
WHERE
help_topic_id DATEDIFF(eDate,date_sub(sDate,interval 1 day))
ORDER BY
help_topic_id
) as s_e_day
) AS a
ON DUPLICATE KEY UPDATE fiscalyear = a.fiscalyear,
fiscalquarter = a.fiscalquarter,
fiscalmonth = a.fiscalmonth,
fiscalweek = a.fiscalweek;
END
假设你的表为 ta 日期字段是 dt
那么,以 2015-01-01为起始日,每5天累总计数为:
select datediff(dt, '2015-01-01') div 5 as d5 , count(*)
from ta
group by (datediff(dt, '2015-01-01') div 5)