MySQL存储过程语句及调用

栏目: 数据库 · 发布时间: 5年前

内容简介:存储过程调用方式:CALL Pro_Get_CO2('2018','','','');
BEGIN
/*
存储过程名称:Pro_Get_Carbon_Climate
存储过程功能说明:不同气候区碳排放占比
创建日期:
涉及的表或试图:
表或试图名称                  用途
存储过程调用:
存储过程名称                  用途
NONE                          
参数说明:
传入参数:
参数名                        		说明
ivF_Year                     			年度
返回字段说明:

调用示例:
CALL Pro_Get_Carbon_Climate(ivF_Year);
CALL Pro_Get_Carbon_Climate('2018');

*/
declare SQL1 varchar(5000);
declare SQL2 varchar(5000);
declare SQLTotal varchar(5000);
declare SQLQuery varchar(5000);
	-- 一次能源碳排放总量
	SET SQL1='select t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year, 
				sum(t2.F_Value*t3.F_NCV*t3.F_EF) as TPF_HL,sum(t2.F_Value*t3.F_Formular) as ZBM
				FROM t_organization t1
				join t_energy_data t2 ON t1.F_ID = t2.F_OrgID 
				join v_zjpflist t3 on t2.F_FieldID=t3.F_FieldID
				where t3.F_IsSum=1  ';
	SET SQL1 = CONCAT(SQL1,' and t2.F_Year= ', ivF_Year);		
	-- 二次能源碳排放总量
	SET SQL2='select t1.F_ID, t2.F_Year,
			sum(CASE WHEN t3.F_CalcType = 2 THEN(t2.F_Value * t3.F_EF)END )  as TPF_DL,
			sum(CASE WHEN t3.F_CalcType = 3 THEN(t2.F_Value * t3.F_EF) END )  as TPF_RL,
			sum(t2.F_Value*t3.F_Formular) as ZBM
			FROM t_organization t1
			join t_energy_data t2 ON t1.F_ID = t2.F_OrgID
			join v_jjpflist t3 on t2.F_FieldID=t3.F_FieldID AND t1.F_ID =T3.F_OrgID
			where t3.F_IsSum=1  ';
	SET SQL2 = CONCAT(SQL2,' and t2.F_Year=', ivF_Year);	

  	SET SQL1 = CONCAT(SQL1,' group by t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year ');
  	SET SQL2 = CONCAT(SQL2,' group by t1.F_ID, t2.F_Year ');

  	#SELECT SQL1;# 输出拼接 sql 语句,相当于print
	#SELECT SQL2;# 输出拼接sql语句,相当于print
  	SET SQLTotal='select AA.F_Climate, SUM(AA.F_BuildArea) AS F_BuildArea, SUM(AA.F_EnergyPerson) AS F_EnergyPerson,
  					SUM(AA.ZBM + BB.ZBM) AS F_TotalZBM,
					SUM(AA.TPF_HL + BB.TPF_DL + BB.TPF_RL) AS F_TotalTPF,
					SUM(AA.TPF_HL) AS TPF_HL,
					SUM(BB.TPF_DL) AS TPF_DL,
					SUM(BB.TPF_RL) AS TPF_RL ';		
	SET SQLTotal = CONCAT(SQLTotal,' from (', SQL1,') AA JOIN (',SQL2,') BB ON AA.F_ID=BB.F_ID  AND AA.F_Year=BB.F_Year ');		
	SET SQLTotal = CONCAT(SQLTotal,' GROUP BY AA.F_Climate ');
	#SELECT SQLTotal;
	SET SQLQuery ='select BBB.F_Climate, BBB.F_Name, AAA.F_TotalTPF ';
	SET SQLQuery = CONCAT(SQLQuery,' from (select a.F_Code as F_Climate ,a.F_Name,a.F_Order from t_dt_datadict a where a.F_DictType=2) BBB LEFT JOIN (',SQLTotal,') AAA ON AAA.F_Climate=BBB.F_Climate ');
	SET SQLQuery = CONCAT(SQLQuery,' ORDER BY BBB.F_Order ');
	#SELECT SQLQuery;
   SET @SQLQuery = SQLQuery;
  	prepare s1  from @SQLQuery;  
  	EXECUTE s1 ;  
 	deallocate prepare s1 ; 
					
END
BEGIN
/*
存储过程名称:Pro_Get_CO2
存储过程功能说明:不同类型机构碳排放占比
创建日期:
涉及的表或试图:
表或试图名称                  用途
存储过程调用:
存储过程名称                  用途
NONE                          
参数说明:
传入参数:
参数名                        		说明
ivF_Year                     			年度
返回字段说明:

调用示例:
CALL Pro_Get_Carbon_OrgType(ivF_Year);
CALL Pro_Get_Carbon_OrgType('2018');

*/
declare SQL1 varchar(5000);
declare SQL2 varchar(5000);
declare SQLTotal varchar(5000);
declare SQLQuery varchar(5000);
	-- 一次能源碳排放总量
	SET SQL1='select t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year, 
				sum(t2.F_Value*t3.F_NCV*t3.F_EF) as TPF_HL,sum(t2.F_Value*t3.F_Formular) as ZBM
				FROM t_organization t1
				join t_energy_data t2 ON t1.F_ID = t2.F_OrgID 
				join v_zjpflist t3 on t2.F_FieldID=t3.F_FieldID
				where t3.F_IsSum=1  ';
	SET SQL1 = CONCAT(SQL1,' and t2.F_Year= ', ivF_Year);		
	-- 二次能源碳排放总量
	SET SQL2='select t1.F_ID, t2.F_Year,
			sum(CASE WHEN t3.F_CalcType = 2 THEN(t2.F_Value * t3.F_EF)END )  as TPF_DL,
			sum(CASE WHEN t3.F_CalcType = 3 THEN(t2.F_Value * t3.F_EF) END )  as TPF_RL,
			sum(t2.F_Value*t3.F_Formular) as ZBM
			FROM t_organization t1
			join t_energy_data t2 ON t1.F_ID = t2.F_OrgID
			join v_jjpflist t3 on t2.F_FieldID=t3.F_FieldID AND t1.F_ID =T3.F_OrgID
			where t3.F_IsSum=1  ';
	SET SQL2 = CONCAT(SQL2,' and t2.F_Year=', ivF_Year);	

  	SET SQL1 = CONCAT(SQL1,' group by t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year ');
  	SET SQL2 = CONCAT(SQL2,' group by t1.F_ID, t2.F_Year ');

  	#SELECT SQL1;# 输出拼接sql语句,相当于print
	#SELECT SQL2;# 输出拼接sql语句,相当于print
  	SET SQLTotal='select AA.F_OrgType, SUM(AA.F_BuildArea) AS F_BuildArea, SUM(AA.F_EnergyPerson) AS F_EnergyPerson,
  					SUM(AA.ZBM + BB.ZBM) AS F_TotalZBM,
					SUM(AA.TPF_HL + BB.TPF_DL + BB.TPF_RL) AS F_TotalTPF,
					SUM(AA.TPF_HL) AS TPF_HL,
					SUM(BB.TPF_DL) AS TPF_DL,
					SUM(BB.TPF_RL) AS TPF_RL ';		
	SET SQLTotal = CONCAT(SQLTotal,' from (', SQL1,') AA JOIN (',SQL2,') BB ON AA.F_ID=BB.F_ID  AND AA.F_Year=BB.F_Year ');		
	SET SQLTotal = CONCAT(SQLTotal,' GROUP BY AA.F_OrgType ');
	#SELECT SQLTotal;
	SET SQLQuery ='select BBB.F_OrgType, BBB.F_Name, AAA.F_TotalTPF ';
	SET SQLQuery = CONCAT(SQLQuery,' from (select a.F_Code as F_OrgType ,a.F_Name,a.F_Order from t_dt_datadict a where a.F_DictType=1) BBB LEFT JOIN (',SQLTotal,') AAA ON AAA.F_OrgType=BBB.F_OrgType ');
	SET SQLQuery = CONCAT(SQLQuery,' ORDER BY BBB.F_Order ');
	#SELECT SQLQuery;
   SET @SQLQuery = SQLQuery;
  	prepare s1  from @SQLQuery;  
  	EXECUTE s1 ;  
 	deallocate prepare s1 ; 
					
END
BEGIN
/*
存储过程名称:Pro_Get_CO2
存储过程功能说明:获得年度统计分析(碳排放,折标煤)
创建日期:
涉及的表或试图:
表或试图名称                  用途
存储过程调用:
存储过程名称                  用途
NONE                          
参数说明:
传入参数:
参数名                        		说明
F_Year                     			年度
F_ProvinceCode                      地区
F_Climate                   			气候区
F_OrgType                        	机构类型
返回字段说明:

调用示例:
CALL Pro_Get_CO2(F_Year,F_ProvinceCode,F_Climate,F_OrgType);
CALL Pro_Get_CO2('2018','','','');

*/

declare SQL1 varchar(5000);
declare SQL2 varchar(5000);
declare SQLTotal varchar(5000);
-- 一次能源碳排放总量
SET SQL1='select t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year, 
				sum(t2.F_Value*t3.F_NCV*t3.F_EF) as TPF_HL,sum(t2.F_Value*t3.F_Formular) as ZBM
				FROM t_organization t1
				join t_energy_data t2 ON t1.F_ID = t2.F_OrgID 
				join v_zjpflist t3 on t2.F_FieldID=t3.F_FieldID
				where t3.F_IsSum=1  ';

-- 二次能源碳排放总量
SET SQL2='select t1.F_ID, t2.F_Year,
			sum(CASE WHEN t3.F_CalcType = 2 THEN(t2.F_Value * t3.F_EF)END )  as TPF_DL,
			sum(CASE WHEN t3.F_CalcType = 3 THEN(t2.F_Value * t3.F_EF) END )  as TPF_RL,
			sum(t2.F_Value*t3.F_Formular) as ZBM
			FROM t_organization t1
			join t_energy_data t2 ON t1.F_ID = t2.F_OrgID
			join v_jjpflist t3 on t2.F_FieldID=t3.F_FieldID AND t1.F_ID =T3.F_OrgID
			where t3.F_IsSum=1  ';
			
  IF F_Year IS NOT NULL AND F_Year <> '' THEN 
    SET SQL1 = CONCAT(SQL1,' and t2.F_Year=', F_Year);
    SET SQL2 = CONCAT(SQL2,' and t2.F_Year=', F_Year);
  END IF;
  
  IF F_ProvinceCode IS NOT NULL AND F_ProvinceCode <> '' THEN   
    SET SQL1 = CONCAT(SQL1,' and t1.F_ProvinceCode=''', F_ProvinceCode,''' ');
    SET SQL2 = CONCAT(SQL2,' and t1.F_ProvinceCode=''', F_ProvinceCode,''' ');
  END IF;
  IF F_Climate IS NOT NULL AND F_Climate <> '' THEN 
    SET SQL1 = CONCAT(SQL1,' and t1.F_Climate=''', F_Climate,''' ');
    SET SQL2 = CONCAT(SQL2,' and t1.F_Climate=''', F_Climate,''' ');
  END IF;
  
  IF F_OrgType IS NOT NULL AND F_OrgType <> '' THEN 
    SET SQL1 = CONCAT(SQL1,' and t1.F_OrgType=''', F_OrgType,''' ');
    SET SQL2 = CONCAT(SQL2,' and t1.F_OrgType=''', F_OrgType,''' ');
  END IF; 

  SET SQL1 = CONCAT(SQL1,' group by t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year ');
  SET SQL2 = CONCAT(SQL2,' group by t1.F_ID, t2.F_Year ');

  #SELECT SQL1;# 输出拼接sql语句,相当于print
  #SELECT SQL2;# 输出拼接sql语句,相当于print
  
  SET SQLTotal='select AA.F_OrgName, AA.F_ProvinceCode,AA.F_Climate, AA.F_OrgType, AA.F_BuildArea, AA.F_EnergyPerson, AA.F_Year, 
						AA.ZBM+BB.ZBM AS F_TotalZBM,
						CASE WHEN AA.F_BuildArea=0 THEN 0 ELSE (AA.ZBM+BB.ZBM )/AA.F_BuildArea END AS F_PerAreaZBM,
						CASE WHEN AA.F_EnergyPerson=0 THEN 0 ELSE (AA.ZBM+BB.ZBM )/AA.F_EnergyPerson END AS F_PerPersonZBM,
						AA.TPF_HL+BB.TPF_DL+BB.TPF_RL AS F_TotalTPF,AA.TPF_HL,BB.TPF_DL,BB.TPF_RL,
						CASE WHEN AA.F_BuildArea=0 THEN 0 ELSE (AA.TPF_HL+BB.TPF_DL+BB.TPF_RL )/AA.F_BuildArea END AS F_PerAreaTPF,
						CASE WHEN AA.F_EnergyPerson=0 THEN 0 ELSE (AA.TPF_HL+BB.TPF_DL+BB.TPF_RL )/AA.F_EnergyPerson END AS F_PerPersonTPF ';		
	SET SQLTotal = CONCAT(SQLTotal,' from (', SQL1,') AA JOIN (',SQL2,') BB ON AA.F_ID=BB.F_ID  AND AA.F_Year=BB.F_Year ');		
	SET SQLTotal = CONCAT(SQLTotal,' ORDER BY AA.F_ID,AA.F_Year ');

	#SELECT SQLTotal;# 输出拼接sql语句,相当于print						
  	SET @SQLTotal = SQLTotal;
  	prepare s1  from @SQLTotal;  
  	EXECUTE s1 ;  
 	deallocate prepare s1 ; 
END
BEGIN
/*
存储过程名称:Pro_Get_CO2
存储过程功能说明:获得近5年的能耗数据,碳排放数据
创建日期:
涉及的表或试图:
表或试图名称                  用途
存储过程调用:
存储过程名称                  用途
NONE                          
参数说明:
传入参数:
参数名                        		说明
ivF_Year                     			年度
返回字段说明:

调用示例:
CALL Pro_Get_EnergyData(ivF_Year);
CALL Pro_Get_EnergyData('2018');

*/
declare SQL0 varchar(5000);
declare SQL1 varchar(5000);
declare SQL2 varchar(5000);
declare SQLTotal varchar(5000);
declare SQLQuery varchar(5000);
	-- 构造近5年列表
	SET SQL0=CONCAT('SELECT @cdate :=@cdate-1 AS F_Year FROM (SELECT @cdate := ',ivF_Year+1,' FROM at_sys_errorlog LIMIT 5 ) t4');
	-- 一次能源碳排放总量
	SET SQL1='select t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year, 
				sum(t2.F_Value*t3.F_NCV*t3.F_EF) as TPF_HL,sum(t2.F_Value*t3.F_Formular) as ZBM
				FROM t_organization t1
				join t_energy_data t2 ON t1.F_ID = t2.F_OrgID 
				join v_zjpflist t3 on t2.F_FieldID=t3.F_FieldID
				where t3.F_IsSum=1  ';
	SET SQL1 = CONCAT(SQL1,' and t2.F_Year>= ', ivF_Year-4,' and t2.F_Year<= ', ivF_Year);		
	-- 二次能源碳排放总量
	SET SQL2='select t1.F_ID, t2.F_Year,
			sum(CASE WHEN t3.F_CalcType = 2 THEN(t2.F_Value * t3.F_EF)END )  as TPF_DL,
			sum(CASE WHEN t3.F_CalcType = 3 THEN(t2.F_Value * t3.F_EF) END )  as TPF_RL,
			sum(t2.F_Value*t3.F_Formular) as ZBM
			FROM t_organization t1
			join t_energy_data t2 ON t1.F_ID = t2.F_OrgID
			join v_jjpflist t3 on t2.F_FieldID=t3.F_FieldID AND t1.F_ID =T3.F_OrgID
			where t3.F_IsSum=1  ';
	SET SQL2 = CONCAT(SQL2,' and t2.F_Year>= ', ivF_Year-4,' and t2.F_Year<= ', ivF_Year);	

  	SET SQL1 = CONCAT(SQL1,' group by t1.F_ID, t1.F_OrgName, t1.F_ProvinceCode, t1.F_CityCode, t1.F_Climate, t1.F_OrgType, t1.F_BuildArea, t1.F_EnergyPerson, t2.F_Year ');
  	SET SQL2 = CONCAT(SQL2,' group by t1.F_ID, t2.F_Year ');

  	#SELECT SQL1;# 输出拼接sql语句,相当于print
	#SELECT SQL2;# 输出拼接sql语句,相当于print
  	SET SQLTotal='select AA.F_Year, SUM(AA.F_BuildArea) AS F_BuildArea, SUM(AA.F_EnergyPerson) AS F_EnergyPerson,
  					SUM(AA.ZBM + BB.ZBM) AS F_TotalZBM,
					SUM(AA.TPF_HL + BB.TPF_DL + BB.TPF_RL) AS F_TotalTPF,
					SUM(AA.TPF_HL) AS TPF_HL,
					SUM(BB.TPF_DL) AS TPF_DL,
					SUM(BB.TPF_RL) AS TPF_RL ';		
	SET SQLTotal = CONCAT(SQLTotal,' from (', SQL1,') AA JOIN (',SQL2,') BB ON AA.F_ID=BB.F_ID  AND AA.F_Year=BB.F_Year ');		
	SET SQLTotal = CONCAT(SQLTotal,' GROUP BY AA.F_Year ');
	#SELECT SQLTotal;
	SET SQLQuery ='select BBB.F_Year, AAA.F_BuildArea, AAA.F_EnergyPerson,AAA.F_TotalZBM,
					CASE WHEN AAA.F_BuildArea=0 THEN 0 ELSE AAA.F_TotalZBM/AAA.F_BuildArea END AS F_PerAreaZBM,
					CASE WHEN AAA.F_EnergyPerson=0 THEN 0 ELSE AAA.F_TotalZBM/AAA.F_EnergyPerson END AS F_PerPersonZBM,
					AAA.F_TotalTPF,
					CASE WHEN AAA.F_BuildArea=0 THEN 0 ELSE AAA.F_TotalTPF/AAA.F_BuildArea END AS F_PerAreaTPF,
					CASE WHEN AAA.F_EnergyPerson=0 THEN 0 ELSE AAA.F_TotalTPF/AAA.F_EnergyPerson END AS F_PerPersonTPF ';
	SET SQLQuery = CONCAT(SQLQuery,' from (', SQL0,') BBB LEFT JOIN (',SQLTotal,') AAA ON AAA.F_Year=BBB.F_Year ');
	SET SQLQuery = CONCAT(SQLQuery,' ORDER BY BBB.F_Year ');
	#SELECT SQLQuery;
   SET @SQLQuery = SQLQuery;
  	prepare s1  from @SQLQuery;  
  	EXECUTE s1 ;  
 	deallocate prepare s1 ; 
					
END

存储过程调用方式:

CALL Pro_Get_CO2('2018','','','');
CALL Pro_Get_EnergyData('2017');
CALL Pro_Get_Carbon_OrgType('2014');
CALL Pro_Get_Carbon_Climate('2014');


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

游戏测试精通

游戏测试精通

舒尔茨 / 周学毛 / 清华大学出版社 / 2007-9 / 48.00元

《游戏测试精通》来自3位在游戏测试领域都有着极其丰富经验的专业人员,是亚马逊“五星级”畅销书,也是国内第一本专业级游戏测试经典之作,不仅内容全面、实例丰富,而且讲解透彻、可读性强,并提供多个资源下载和技术支持站点。现如今,游戏产业发展迅猛,游戏测试已成为游戏产品、游戏软件、游戏程序设计与开发不可或缺的环节。《游戏测试精通》主要揭示了如何将软件测试的专业方法运用到游戏产业中,全面涵盖了游戏测试的基本......一起来看看 《游戏测试精通》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具