您的位置:首页 > 数据库 > MySQL

mysql 存储过程 获取统计结果

2011-07-02 08:52 337 查看
CREATE DEFINER = 'root'@'%' PROCEDURE `proc_GetPatientCondition`(
IN iPatientId INTEGER(11),
IN dStartDate DATE,
IN dEndDate DATE
)
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
Declare tempDate date;
Declare tempDayBegin varchar(9);
Declare tempNightBegin varchar(9);
Declare tempDayTimes int;
Declare tempNightTimes int;
Declare tempFiledBRate int;
Declare tempResultLevel int;/* 1,2,3,4 ,when 0 means no result */
Declare tempResultByFlag int; /* 1-by day times per week , 2- by night times per month , 3- by FiledBrate */
Declare tempIsFinish int;
Declare vSQL varchar(2000);
Declare stmt varchar(2100);
Declare tempStartDateWeekday int;
Declare tempWeekIndex int ;

set tempDayTimes = 0;
set tempNightTimes = 0;
set tempFiledBRate = 0;
set tempResultLevel = 0;
set tempResultByFlag = 0;
set tempIsFinish = 0;

/* get the day time begin */
select concat(' ',ifnull(ParaValue,'07:00'),':00') into tempDayBegin from DBparameter
where ParaKey='DayTime';

select concat(' ',ifnull(ParaValue,'20:00'),':00') into tempNightBegin from DBparameter
where ParaKey='NightTime';

/* define temp table to contains all report dates */
Drop TEMPORARY TABLE if exists temp_tb_dates;
Create TEMPORARY TABLE temp_tb_dates
(reportDate date
,theWeek varchar(7)
,theMonth varchar(7)
);

Drop TEMPORARY TABLE if exists temp_tb_diarySumm;
Create TEMPORARY TABLE temp_tb_diarySumm
(reportDate date
,theWeek varchar(7)
,theMonth varchar(7)
,dayTimes int
,nightTimes int
,FiledBRate int
);

set tempDate = dStartDate;
set tempStartDateWeekday = WEEKDAY(dStartDate); /* 0-Monday */
set tempWeekIndex = 1;
while tempDate <= dEndDate do

insert into temp_tb_dates(reportDate,theWeek,theMonth)
VALUE(tempDate, tempWeekIndex ,date_format(tempDate,'%Y-%m') );

if tempStartDateWeekday = 6 then
set tempStartDateWeekday = 0;
set tempWeekIndex = tempWeekIndex + 1;
else
set tempStartDateWeekday = tempStartDateWeekday + 1;
end if;

set tempDate = date_add(tempDate, interval 1 day);
end while;

set vSQL = ' ';
set vSQL=CONCAT(vSQL,' insert into temp_tb_diarySumm');
set vSQL=CONCAT(vSQL,' select * from ');
set vSQL=CONCAT(vSQL,' ( ');

set vSQL=CONCAT(vSQL,' select t0.reportDate ,theWeek , theMonth ');
set vSQL=CONCAT(vSQL,' , ifnull(dayTimes,0) as dayTimes ');
set vSQL=CONCAT(vSQL,' , ifnull(FiledBRate,0) as FiledBRate ');
set vSQL=CONCAT(vSQL,' from ');
set vSQL=CONCAT(vSQL,' ( ');
set vSQL=CONCAT(vSQL,' select reportDate ,theWeek , theMonth ');
set vSQL=CONCAT(vSQL,' from temp_tb_dates ');
set vSQL=CONCAT(vSQL,' ) t0 ');
set vSQL=CONCAT(vSQL,' left join ');
set vSQL=CONCAT(vSQL,' ( ');
set vSQL=CONCAT(vSQL,' select count(recordid) as dayTimes ');
set vSQL=CONCAT(vSQL,' ,date_format(FiledATime,''%Y-%m-%d'') as reportDate ');
set vSQL=CONCAT(vSQL,' from DBdiaryFiledA ');
set vSQL=CONCAT(vSQL,' where patientid=', iPatientId);
set vSQL=CONCAT(vSQL,' and FiledATime >=','', 'concat(date_format(FiledATime,''%Y-%m-%d''),''',tempDayBegin,''')');
set vSQL=CONCAT(vSQL,' and FiledATime < ','', 'concat(date_format(FiledATime,''%Y-%m-%d''),''',tempNightBegin ,''')');
set vSQL=CONCAT(vSQL,' group by reportDate ) t1 ');
set vSQL=CONCAT(vSQL,' on t1.reportDate = t0.reportDate ');
set vSQL=CONCAT(vSQL,' left join ');
set vSQL=CONCAT(vSQL,' ( ');
set vSQL=CONCAT(vSQL,' select ((ifnull(max(FiledB),1) - ifnull(min(FiledB),1)) * 200 ) /( ifnull(max(FiledB),1) + ifnull(min(FiledB),1) ) as FiledBRate ');
set vSQL=CONCAT(vSQL,' ,date_format(gathertime,''%Y-%m-%d'') as reportDate ');
set vSQL=CONCAT(vSQL,' from DBdiaryFiledBb ');
set vSQL=CONCAT(vSQL,' where patientid=', iPatientId);
set vSQL=CONCAT(vSQL,' and FAvailFlag > 0 ');
set vSQL=CONCAT(vSQL,' and gathertime >= ','','concat(date_format(gathertime,''%Y-%m-%d''),''',' 00:00:00',''')');
set vSQL=CONCAT(vSQL,' and gathertime < ','','concat(date_format(date_add(gathertime, interval 1 day),''%Y-%m-%d''),''',' 00:00:00',''')');
set vSQL=CONCAT(vSQL,' group by reportDate ) t3 ');
set vSQL=CONCAT(vSQL,' on t3.reportDate = t0.reportDate ');

set vSQL=CONCAT(vSQL,' ) t ');

set @sqltext:=vSQL;
prepare stmt from @sqltext;
execute stmt;

select MAX(dayTimesWeek) into tempDayTimes
from
(
select sum(dayTimes) as dayTimesWeek
from temp_tb_diarySumm
group by theWeek
) tDay;

select MAX(nightTimesMonth) into tempNightTimes
from
(
select sum(nightTimes) as nightTimesMonth
from temp_tb_diarySumm
group by theMonth
) tNight;

select max(FiledBRate) into tempFiledBRate
from temp_tb_diarySumm;

if tempIsFinish=0 and tempFiledBRate > 30 THEN
set tempIsFinish = 1;
set tempResultByFlag =3;
set tempResultLevel = 3;
end if;

if tempIsFinish=0 and tempFiledBRate <= 30 and tempFiledBRate >= 20 THEN
set tempIsFinish = 1;
set tempResultByFlag =3;
set tempResultLevel = 2;
end if;

if tempIsFinish=0 and tempFiledBRate < 20 and tempFiledBRate > 0 THEN
set tempIsFinish = 1;
set tempResultByFlag =3;
set tempResultLevel = 1;
end if;

select tempResultLevel , tempResultByFlag , tempDayTimes, tempNightTimes ,tempFiledBRate;

Drop TEMPORARY TABLE if exists temp_tb_dates;
Drop TEMPORARY TABLE if exists temp_tb_diarySumm;

END;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: