drop procedure if exists p13;
CREATE PROCEDURE p13 ()
BEGIN
declare MyTotal float DEFAULT 0;
DECLARE MyCount int DEFAULT 0;
select @MyCount:= COUNT(ID)from GeeSun_PLCReadData where FunctionID=13 and IsArray=0 and IsEnable=1 ;
set MyCount=@MyCount;
CREATE TEMPORARY TABLE temp_table AS
select a.*,c.Name,concat(concat(c.Name,'值:'),a.ParamValue) as NG_NameValue
from ( select *
from ( select *
from GeeSun_ChartCCDNG
where CreateTime='2022-10-12 16:33:47.480'
order by ID desc limit MyCount) b
order by b.ID asc limit MyCount
) a
left join (select * from GeeSun_PLCReadData where FunctionID = 13) c on
a.ParamID = c.ID;
select @MyTotal =SUM(ParamValue) from temp_table;
set MyTotal=@MyTotal;
if MyTotal >0
then
SELECT *,CONCAT(CONCAT(Name,':'),CONCAT(round(ParamValue/CAST(MyTotal AS decimal)*100 ,2),'%')) as percentage from temp_table;
else
SELECT *,CONCAT(CONCAT(Name,':'),CONCAT(ParamValue,'%')) as percentage from temp_table;
end if;
drop table temp_table;
end;
call p13;
Mysql中的存储过程相关语句
于 2023-03-31 11:43:02 首次发布
该存储过程首先删除已存在的p13过程,然后创建新过程。过程中声明变量并计算GeeSun_PLCReadData表中满足条件的记录数,接着创建一个临时表,从GeeSun_ChartCCDNG数据中选取部分记录,并与GeeSun_PLCReadData表进行连接。根据计算的总数,如果总数大于0,则计算每个记录的值占总数的百分比,否则直接显示值。最后,过程结束时删除临时表。
摘要由CSDN通过智能技术生成