mysql 日期处理成财年财季财月财周

mysql 日期处理成财年财季财月财周,第1张

财年是一个公司的上市时间决定的,所以不同的公司财年码表不同,因为我目前用的财年是从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

您好,一、年度查询

查询 本年度的数据

SELECT *

FROM blog_article

WHERE year( FROM_UNIXTIME( BlogCreateTime ) ) = year( curdate( ))

二、查询季度数据

查询数据附带季度数

SELECT ArticleId, quarter( FROM_UNIXTIME( `BlogCreateTime` ) )

FROM `blog_article`

其他的同前面部分:查询 本季度的数据

SELECT *

FROM blog_article

WHERE quarter( FROM_UNIXTIME( BlogCreateTime ) ) = quarter( curdate( ))

三、查询月度数据

本月统计(MySQL)

select * from booking where month(booking_time) =

month(curdate()) and year(booking_time) = year(curdate())

本周统计(MySQL)

select * from spf_booking where month(booking_time) =

month(curdate()) and week(booking_time) = week(curdate())

四、时间段

N天内记录

WHERE TO_DAYS(NOW()) - TO_DAYS(时间字段) <= N

当天的记录

where date(时间字段)=date(now())

where to_days(时间字段) = to_days(now())

查询一周:

select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(column_time)

查询一个月:

select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(column_time)

查询'06-03'到'07-08'这个时间段内所有过生日的会员:

Select * From user Where

DATE_FORMAT(birthday,'%m-%d') >= '06-03' and DATE_FORMAT(birthday,'%m-%d')

<= '07-08'

统计一季度数据,表时间字段为:savetime

group by concat(date_format(savetime, '%Y '),FLOOR((date_format(savetime, '%m ')+2)/3))

select YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1,count(*)

from yourTable

group by YEAR(savetime)*10+((MONTH(savetime)-1) DIV 3) +1

五、分组查询

1、年度分组

2、月度分组

3、先按年度分组,再按月度分组

4、按年月分组

SELECT count(ArticleId), date_format(FROM_UNIXTIME( `BlogCreateTime`),'%y%m') sdate FROM `blog_article` group by sdate

结果:

count( ArticleId ) sdate

17 0901

11 0902

5 0903

6 0904

2 0905

1 0907

12 0908

6 0909

11 0910

3 0911


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/8345801.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-15
下一篇 2023-04-15

发表评论

登录后才能评论

评论列表(0条)

保存