# 在編寫存儲過程時,可能會遇到通過輸入的時間,得出該時間段的年月
# 下面通過試圖的方式解決該問題
# 思路:
# 創建 年 、 月 的試圖
# 這里創建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;