(续)
-- 统计SJ级别的数量
SELECT
CONCAT(DATE_FORMAT(t_sj.tjsj,'%Y%m'),'43XXXXXXXX00','018',xmid,'000') AS id,-- 部门id
'43XXXXXXXX00' AS ssqxdm,
DATE_FORMAT(t_sj.tjsj,'%Y-%m') AS tjyf,
( -- 当前月份的统计数量
SELECT
SUM(CASE WHEN t_tjsl.tjsl IS NULL THEN 0 ELSE t_tjsl.tjsl END)
FROM
tmp_table_79_80_81 t_tjsl
WHERE
t_tjsl.tjsj= t_sj.tjsj
)AS tjsl,
( -- 统计去年同月数量
SELECT
SUM(CASE WHEN t_qntytjsl.tjsl IS NULL THEN 0 ELSE t_qntytjsl.tjsl END)
FROM
tmp_table_79_80_81 t_qntytjsl
WHERE
t_qntytjsl.tjsj= SUBDATE(t_sj.tjsj,INTERVAL 1 YEAR)
)AS qntytjsl,
(-- 统计今年累计数量
SELECT
SUM( CASE WHEN t_jnlj.tjsl IS NULL THEN 0 ELSE t_jnlj.tjsl END )
FROM
tmp_table_79_80_81 t_jnlj
WHERE
t_jnlj.tjsj >= STR_TO_DATE(CONCAT(YEAR(t_sj.tjsj),'-01-01'),'%Y-%m-%d')
AND t_jnlj.tjsj <=t_sj.tjsj
)AS jnlj,
(-- 统计去年累计数量
SELECT
SUM( CASE WHEN t_qnlj.tjsl IS NULL THEN 0 ELSE t_qnlj.tjsl END )
FROM
tmp_table_79_80_81 t_qnlj
WHERE
t_qnlj.tjsj >= STR_TO_DATE(CONCAT(YEAR(SUBDATE(t_sj.tjsj,INTERVAL 1 YEAR)),'-01-01'),'%Y-%m-%d')
AND t_qnlj.tjsj <= SUBDATE(t_sj.tjsj,INTERVAL 1 YEAR)
)AS qnlj,
(-- 统计去年全年累计
SELECT
SUM( CASE WHEN t_qnqntj.tjsl IS NULL THEN 0 ELSE t_qnqntj.tjsl END )
FROM
tmp_table_79_80_81 t_qnqntj
WHERE
t_qnqntj.tjsj >= STR_TO_DATE(CONCAT(YEAR(SUBDATE(t_sj.tjsj,INTERVAL 1 YEAR)),'-01-01'),'%Y-%m-%d')
AND t_qnqntj.tjsj <= STR_TO_DATE(CONCAT(YEAR(SUBDATE(t_sj.tjsj,INTERVAL 1 YEAR)),'-12-01'),'%Y-%m-%d')
)AS qnqntj
FROM
(
SELECT
sj.tjsj
FROM
tmp_table_79_80_81 sj
WHERE
CASE WHEN countdatetime='0' -- 根据传入的参数进行判断统计时间范围
THEN 1=1
ELSE
DATE_FORMAT(sj.tjsj,'%Y%m')=countdatetime
END
GROUP BY
sj.tjsj
)t_sj
)tall
LEFT JOIN
(
SELECT
id AS ssqxdm,
org_name AS ssqxmc,
parent_id AS lydwdm,
(
SELECT org_name FROM 表C org2 WHERE org2.id=org1.parent_id
) AS lydwmc
FROM
gb_ztfx.`t_acl_org` org1
) tacl
ON tall.ssqxdm=tacl.ssqxdm
WHERE tacl.ssqxdm IS NOT NULL;
-- 查询统计数据并赋值给游标结束
-- 声明异常处理
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
-- 捕获异常信息
GET DIAGNOSTICS CONDITION 1 ERROR_CODE=RETURNED_SQLSTATE,ERROR_MSG=MESSAGE_TEXT;
-- 统计错误次数
SET tmp_error_num=tmp_error_num+1;
SET ERROR_PROCEDURE_INFO=CONCAT('{"PROCEDURE_NAME":','"P_79_80_81_count";'
,'"countdatetime":"',countdatetime,',"xmid":"',xmid,'"}');
INSERT INTO
t_ztfx_ywtj_procedure_error_log(error_code,error_msg,error_procedure_info,error_create_time)
VALUES(ERROR_CODE,ERROR_MSG,ERROR_PROCEDURE_INFO,DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s'));
END;
-- 将结束标志绑定到游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET cur_account_finished = 1;
-- 打开游标
OPEN cur_account;
-- 统计计数置为0
SET count_num=0;
-- 遍历游标,取出数据,写入临时表
read_loop:LOOP
-- 进行遍历 字段赋值
FETCH NEXT FROM cur_account
INTO temp_id,temp_tjyf,temp_ssqxdm,temp_tjsl,temp_qntytjsl,temp_jnlj,
temp_qnlj,temp_qnqntj,temp_ssqxmc,temp_lydwdm,temp_lydwmc,
temp_lbid,temp_lb,temp_xmid,temp_xm,temp_xmxlid,temp_xmxl;
-- 判断是否结束
IF cur_account_finished = 1 THEN
-- 判断是否刚好到达提交临界点
IF temp_detele_idstr <>'' THEN
-- 根据id批量删除数据
SET temp_detele_idstr=SUBSTR(temp_detele_idstr,0,LENGTH(temp_detele_idstr)-1);
SET @temp_detele_sql=CONCAT('DELETE FROM 统计表 tjtb WHERE tjtb.id IN ',
'(',temp_detele_idstr,')');
PREPARE delete_value_stmt FROM @temp_detele_sql;
EXECUTE delete_value_stmt;
-- 批量插入数据 防止有逗号在最后,使用0作消除。
SET temp_insert_str=SUBSTRING(temp_insert_str,0,LENGTH(temp_insert_str)-1);
SET @temp_insert_sql=CONCAT('INSERT INTO 统计表(',
'id,lbid,lb,xmid,xm,xmxlid,xmxl,tjyf,ssqxdm,ssqxmc,tjsl,qntytjsl,jnlj,qnlj,qnqntj,lydwdm,lydwmc,tjsj',
') VALUES ',temp_insert_str,'(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)');
PREPARE insert_value_stmt1 FROM @temp_insert_sql;
EXECUTE insert_value_stmt1;
-- 删除id为‘0’的数据
DELETE FROM 统计表 tjtb WHERE tjtb.id='0';
END IF;
LEAVE read_loop;
END IF;
-- 判断批量提交的次数
IF temp_update_insert_count%update_insert_num = 0 THEN
-- 批量删除数据
SET temp_detele_idstr=SUBSTR(temp_detele_idstr,0,LENGTH(temp_detele_idstr)-1);
SET @temp_detele_sql=CONCAT('DELETE FROM 统计表 tjtb WHERE tjtb.id IN ',
'(',temp_detele_idstr,')');
PREPARE delete_value_stmt FROM @temp_detele_sql;
EXECUTE delete_value_stmt;
-- 批量插入数据
SET @temp_insert_sql=CONCAT('INSERT INTO 统计表(',
'id,lbid,lb,xmid,xm,xmxlid,xmxl,tjyf,ssqxdm,ssqxmc,tjsl,qntytjsl,jnlj,qnlj,qnqntj,lydwdm,lydwmc,tjsj',
') VALUES ',temp_insert_str);
PREPARE insert_value_stmt2 FROM @temp_insert_sql;
EXECUTE insert_value_stmt2;
-- 重置操作字符串
SET temp_detele_idstr='';
SET temp_insert_str='';
END IF;
-- 拼接批量id
SET temp_detele_idstr=CONCAT(temp_detele_idstr,'\'',temp_id,'\',');
-- 拼接批量插入字符串
SET temp_time= DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s');
SET temp_insert_str=CONCAT(temp_insert_str,'(','\'',temp_id,'\',\'',temp_lbid,'\',\'',temp_lb,'\',\'',
temp_xmid,'\',\'',temp_xm,'\',\'',IFNULL(temp_xmxlid,''),'\',\'',IFNULL(temp_xmxl,''),'\',\'',temp_tjyf,'\',\'',
IFNULL(temp_ssqxdm,''),'\',\'',IFNULL(temp_ssqxmc,''),'\',',IFNULL(temp_tjsl,0),',',
IFNULL(temp_qntytjsl,0),',',IFNULL(temp_jnlj,0),',',IFNULL(temp_qnlj,0),',',
IFNULL(temp_qnqntj,0),',\'',IFNULL(temp_lydwdm,''),'\',\'',IFNULL(temp_lydwmc,''),'\',\'',temp_time,'\')',
CASE WHEN (temp_update_insert_count+1)%update_insert_num = 0 THEN '' ELSE ',' END);
-- 计数器+1
SET temp_update_insert_count=temp_update_insert_count + 1;
SET count_num=count_num+1;
-- 结束循环
END LOOP;
-- 关闭游标
CLOSE cur_account;
SET error_num=tmp_error_num;
END