MySQL 获取指定时间段的年月

# 在编写存储过程时,可能会遇到通过输入的时间,得出该时间段的年月
# 下面通过试图的方式解决该问题
# 思路:
# 创建 年 、 月  的试图

# 这里创建2010~2020 年的年份
drop view if exists v_year; 
CREATE VIEW v_year AS (SELECT '2010' as yearname) 
UNION ALL
	(SELECT '2011')
UNION ALL
	(SELECT '2012')
UNION ALL
	(SELECT '2013')
UNION ALL
	(SELECT '2014')
UNION ALL
	(SELECT '2015')
UNION ALL
	(SELECT '2016')
UNION ALL
	(SELECT '2017')
UNION ALL
	(SELECT '2018')
UNION ALL
	(SELECT '2019')
UNION ALL
	(SELECT '2020') 
;

#  创建1~12 月的月份
drop view if exists v_month; 
CREATE VIEW v_month AS (SELECT '01' as monthname)
UNION ALL
	(SELECT '02')
UNION ALL
	(SELECT '03')
UNION ALL
	(SELECT '04')
UNION ALL
	(SELECT '05')
UNION ALL
	(SELECT '06')
UNION ALL
	(SELECT '07')
UNION ALL
	(SELECT '08')
UNION ALL
	(SELECT '09')
UNION ALL
	(SELECT '10')
UNION ALL
	(SELECT '11')
UNION ALL
	(SELECT '12')
;

# 将 以上创建的两个试图 作为另一个试图(包含年月的试图)的基础数据
drop view if exists v_year_month;
create view v_year_month as (
select * from v_year,v_month
);

 

# 视图 v_year_month 中 包含了2010~2020年 和对应的 1~12月 的数据 132 条

#  下面是学习中写的一个存储过程 
#  其中表是按照月份创建的,例如格式为:testrecored201703 表示2017年03月的数据表 
#  如果参数为 2017-05-09 11:28:27  2017-11-09 11:28:39 则 需要查询
#  testrecored201705、testrecored201706、testrecored201707、testrecored201708、testrecored201709 
#  所以 需要得到201705 、 201706 、201707 、201708 、 201709 通过游标可遍历

  

drop procedure if exists pro40000;
delimiter //
create procedure pro40000(startdatetime datetime,enddatetime datetime)
begin 
declare yearmonth varchar(10);
declare normhourtable varchar(50);
declare normnormtable varchar(50);
DECLARE	done INT DEFAULT FALSE;	
DECLARE cursor_yearmonth CURSOR FOR 
SELECT concat_ws('',yearname,monthname) as yearmonth from v_year_month 
where yearname BETWEEN DATE_FORMAT(startdatetime,'%Y') and DATE_FORMAT(enddatetime,'%Y')
and monthname BETWEEN DATE_FORMAT(startdatetime,'%m') and DATE_FORMAT(enddatetime,'%m');
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;	
TRUNCATE table t_internetsumrecord;
OPEN cursor_yearmonth; 
read_loop: LOOP
FETCH cursor_yearmonth INTO yearmonth; 
IF done THEN
LEAVE read_loop;
END IF;

set normhourtable = concat('internetsumrecord_hour',yearmonth);
set normnormtable = concat('internetsumrecord_norm_hour',yearmonth);
set @SQL = CONCAT('insert into t_internetsumrecord ',
'SELECT ctinfo.NAMECN, LOGINUSERNAME,
cast(sum(case when normhour.normid=6 then normhour.normtotal else 0 end)
/sum(case when normhour.normid=6 then normhour.normcount else 0 end) as decimal(10,2)) as pingOffSetTime,
cast(sum(case when normhour.normid=171 then normhour.normtotal else 0 end)
/sum(case when normhour.normid=171 then normhour.normcount else 0 end) / 1000 as decimal(10,2)) as firstScreenTime,
cast(sum(case when normhour.normid=83 then normhour.normtotal else 0 end)
/sum(case when normhour.normid=83 then normhour.normcount else 0 end) / 1000 as decimal(10,2)) as netShowOffSetTime from ',normhourtable,
' sumrecord LEFT JOIN ',normnormtable,' normhour on normhour.SUPERID = sumrecord.ID',
' left join ctinfo on sumrecord.ctid=ctinfo.ctid where sumrecord.servicetype=','''400''',' and sumrecord.subservicetype=','''00''',
' and sumrecord.expservicetype=','''0''',' GROUP BY ctinfo.NAMECN,LOGINUSERNAME LIMIT ',0,',',20);
# SELECT @SQL;
PREPARE stms from @SQL;
EXECUTE stms;
DEALLOCATE PREPARE stms;
END LOOP;
CLOSE cursor_yearmonth; -- 关闭游标
end
//
delimiter;

  

posted @ 2017-11-09 11:34  ltang0  阅读(6357)  评论(0编辑  收藏  举报