mysql>set @last_id := -1
Query OK, 0 rows affected (0.00 sec)
mysql>select id, A, B, result
->from
->(
->select
->table1.*,
->@last_id,
->if(@last_id <0, null, id - @last_id) as result,
->@last_id := id
->from
->table1
->) as tmp
->
+----+------+------+--------+
| id | A| B| result |
+----+------+------+--------+
| 1 |2 |1 | NULL |
| 21 |1 |1 | 20 |
| 33 |3 |2 | 12 |
+----+------+------+--------+
3 rows in set (0.00 sec)
财年是一个公司的上市时间决定的,所以不同的公司财年码表不同,因为我目前用的财年是从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 group_date groupDate from (SELECT
@cdate input_date,
@last_sat := date_add(date_sub(date_sub(@cdate,interval day(@cdate)-1 day),interval 1 month), interval 6-date_format(date_sub(date_sub(@cdate,interval day(@cdate)-1 day),interval 1 month),'%w') day) last_sat,
@this_sat := date_add(date_sub(@cdate,interval day(@cdate)-1 day), interval 6-date_format(date_sub(@cdate,interval day(@cdate)-1 day),'%w') day) this_sat,
@next_sat := date_add(date_add(date_sub(@cdate,interval day(@cdate)-1 day),interval 1 month), interval 6-date_format(date_add(date_sub(@cdate,interval day(@cdate)-1 day),interval 1 month),'%w') day) next_sat,
@last_point := if(day(@last_sat)-3>0,date_sub(@last_sat,interval 7 day),@last_sat) last_point,
@this_point := if(day(@this_sat)-3>0,date_sub(@this_sat,interval 7 day),@this_sat) this_point,
@next_point := if(day(@next_sat)-3>0,date_sub(@next_sat,interval 7 day),@next_sat) next_point ,
case
when @cdate >= @last_point and @cdate < @this_point then
concat(
'W',
datediff(@cdate,@last_point) div 7 + 1, '/',
year(@cdate),'-',
if(month(@last_sat)>=10,month(@last_sat),concat('0',month(@last_sat)))
)
when @cdate >=@this_point and @cdate < @next_point then
concat(
'W',
datediff(@cdate,@this_point) div 7 + 1,'/',
year(@cdate),'-',
if(month(@this_sat)>=10,month(@this_sat),concat('0',month(@this_sat))))
else
concat(
'W',
datediff(@cdate,@next_point) div 7 + 1,'/',
year(@cdate),'-',if(month(@next_sat)>=10,month(@next_sat),concat('0',month(@next_sat))))
end
group_date
from (SELECT @cdate := #{inputDate}) as gd
);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)