Advertisement
Guest User

Untitled

a guest
May 27th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.41 KB | None | 0 0
  1. /*
  2. 2013年绩效考核结果
  3. mysql 临时表不能在一次查询中连续引用两次
  4. select a.id,b.id from rpt a,rpt b where a.id=b.id会出错
  5. */
  6.  
  7. drop procedure if exists yearchk2013;
  8. create procedure yearchk2013 (in i_year bigint(20))
  9. begin
  10.  
  11. declare Vdigits int(10); -- 小数位数
  12. set Vdigits=6;
  13.  
  14.  
  15. drop table if exists rpt;
  16. create temporary table rpt
  17. (
  18. id int(12) not null default '0',
  19. name varchar(20) default null,
  20. department varchar(255) default null,
  21. no int(12) default null,-- 最后序号
  22. scheck decimal(10,6) default null,-- 部门经理得分
  23. mdep decimal(10,6) default null,-- 部门平均得分
  24. mall decimal(10,6) default null,-- 总平均
  25. stotal decimal(10,6) default 150,
  26. s decimal(10,6) default null, -- s:计算中间值
  27. savg decimal(10,6) default null, -- s平均值
  28. damp decimal(10,6) default null,-- 部门分值幅度
  29. mamp decimal(10,6) default null,-- 总体分值幅度
  30. alpha decimal(10,6) default null,-- 中间参数
  31. upsilon decimal(10,6) default null,-- 中间参数
  32. val decimal(10,6) default null,-- 横向得分
  33. vavg decimal(10,6) default null,-- v的平均值
  34. iavg decimal(10,6) default null,-- 平均参与度
  35. iabs decimal(10,6) default null,-- 绝对参与度
  36. idir decimal(10,6) default null,-- 有想参与度
  37. ibeta decimal(10,6) default 0.2,-- 参与度参数
  38. iv decimal(10,6) default null,-- 参与度得分
  39. result decimal(10,6) default null,-- 计算结果
  40. primary key (id)
  41. ) engine = memory;
  42.  
  43. -- 插入人员姓名和部门得分
  44. insert into rpt(id,name,department,scheck)
  45. select person.id,person.name,person.department,realpt from fyperson person,(select a.personid,round(sum(a.val*b.point)/5,Vdigits) as realpt
  46. from fychkmange a,fychkitem b where a.itemid=b.id and a.year=i_year group by a.personid) temp
  47. where person.id=temp.personid order by person.id asc;
  48.  
  49.  
  50.  
  51. -- 计算部门平均
  52. begin
  53. declare Vdepartment varchar(255) default null;-- 部门
  54. declare Vmdep decimal(10,6) default null;-- 部门平均分
  55. declare done int(10) default false;-- 遍历数据结束标志
  56.  
  57. declare cur_dep cursor for select department,sum(scheck)/count(id) as mdep from rpt group by department;
  58. declare continue handler for not found set done = true;
  59.  
  60. open cur_dep;
  61. mdep_loop: loop
  62. fetch cur_dep into Vdepartment,Vmdep;
  63. if done then
  64. leave mdep_loop;
  65. end if;
  66. update rpt set mdep=Vmdep where rpt.department=Vdepartment;
  67. end loop;
  68. close cur_dep;
  69. end;
  70.  
  71. -- 计算总体平均
  72. begin
  73. declare Vmall decimal(10,6);
  74. select sum(scheck)/count(id) into Vmall from rpt;
  75. update rpt set mall=Vmall;
  76. end;
  77.  
  78.  
  79. /**
  80. * 根据提供的值,计算s 公式如下
  81. * [Scheck+(Mall-Mdep)]/Stotal
  82. *
  83. * 个人得分 + ( 员工平均得分 - 部门平均分 )
  84. * -----------------------------------
  85. * 总分
  86. */
  87. update rpt set s=round((scheck+mall-mdep)/stotal,Vdigits);
  88.  
  89. -- 计算s平均值savg
  90. begin
  91. declare Vsavg decimal(10,6);
  92. select sum(s)/count(id) into Vsavg from rpt;
  93. update rpt set savg=Vsavg;
  94. end;
  95.  
  96. -- 部门分值幅度
  97. begin
  98. declare Vdepartment varchar(255) default null;-- 部门
  99. declare Vdamp decimal(10,6) default null;-- 部门平均分
  100. declare done int(10) default false;-- 遍历数据结束标志
  101.  
  102. declare cur_damp cursor for select department,round(max(scheck)-min(scheck),Vdigits) as damp from rpt group by department;
  103. declare continue handler for not found set done = true;
  104.  
  105. open cur_damp;
  106. damp_loop:loop
  107. fetch cur_damp into Vdepartment,Vdamp;
  108. if done then
  109. leave damp_loop;
  110. end if;
  111. update rpt set damp=Vdamp where department=Vdepartment;
  112. end loop;
  113. close cur_damp;
  114.  
  115. end;
  116.  
  117. -- 公司平均分值幅度
  118. -- 幅度为0的部门不计算在内
  119. begin
  120. declare Vdamp decimal(10,6) default null;-- 部门平均分
  121. declare Vtotal decimal(10,6) default 0;-- 总分
  122. declare Vnum int(10) default 0;-- 个数
  123.  
  124. declare done int(10) default false;-- 遍历数据结束标志
  125. declare cur_damp cursor for select round(max(scheck)-min(scheck),Vdigits) as damp from rpt group by department;
  126. declare continue handler for not found set done = true;
  127.  
  128. open cur_damp;
  129. damp_loop : loop
  130. fetch cur_damp into Vdamp;
  131. if done then
  132. leave damp_loop;
  133. end if;
  134. if Vdamp>0 then
  135. set Vtotal=Vtotal+Vdamp;
  136. set Vnum=Vnum+1;
  137. end if;
  138. end loop;
  139. close cur_damp;
  140.  
  141. update rpt set mamp=round(Vtotal/Vnum,Vdigits);
  142.  
  143. end;
  144.  
  145. -- 计算upsilon
  146. -- alpha=Mamp/Stotal;
  147. -- (S-Savg)*alpha*100
  148. begin
  149. update rpt set alpha=round(mamp/stotal,Vdigits);
  150. update rpt set upsilon=round((s-savg)*alpha*100,Vdigits);
  151. end;
  152.  
  153. -- 计算横向评价得分
  154. begin
  155. declare Vid int(12) default null;
  156. declare Vval decimal(10,6) default null;
  157.  
  158. declare done int(10) default false;-- 遍历数据结束标志
  159. declare cur_val cursor for select personid,round(sum(val)/count(personid)/2*100,Vdigits) from (
  160. select colid as personid, val+1 as val from fycheck where year=i_year
  161. union all
  162. select rowid as personid,-val+1 as val from fycheck where year=i_year
  163. )temp group by personid;
  164. declare continue handler for not found set done = true;
  165.  
  166. open cur_val;
  167. val_loop:loop
  168. fetch cur_val into Vid,Vval;
  169. if done then
  170. leave val_loop;
  171. end if;
  172. update rpt set val=Vval where id=Vid;
  173. end loop;
  174. close cur_val;
  175. end;
  176.  
  177.  
  178. -- 计算val的平均值
  179. begin
  180. declare Vvavg decimal(10,6);
  181. select round(sum(ifnull(val,0))/count(id),Vdigits) into Vvavg from rpt;
  182. update rpt set vavg=Vvavg;
  183. end;
  184.  
  185.  
  186. -- 平均参与度
  187. begin
  188. declare Viavg decimal(10,6);
  189. select round(sum(abs(val))/count(id),Vdigits) into Viavg from fycheck where year=i_year;
  190. update rpt set iavg=Viavg;
  191. end;
  192.  
  193. -- 绝对参与度
  194. begin
  195. declare Vid int(12) default null;
  196. declare Viabs decimal(10,6) default null;
  197.  
  198. declare done int(10) default false;-- 遍历数据结束标志
  199. declare cur_iabs cursor for select chkid,round(sum(abs(val))/count(id),Vdigits) from fycheck where year=i_year group by chkid;
  200. declare continue handler for not found set done = true;
  201.  
  202. open cur_iabs;
  203. iabs_loop:loop
  204. fetch cur_iabs into Vid,Viabs;
  205. if done then
  206. leave iabs_loop;
  207. end if;
  208. update rpt set iabs=Viabs where id=Vid;
  209. end loop;
  210. close cur_iabs;
  211.  
  212. end;
  213.  
  214. -- 有向参与度
  215. -- idir=iabs-iavg
  216. -- iv=idir*ibeta
  217. update rpt set idir=ifnull(iabs,0)-ifnull(iavg,0);
  218. update rpt set iv=round(vavg*idir*ibeta,Vdigits);
  219.  
  220.  
  221.  
  222. -- result=upsilon+val;
  223. update rpt set result=upsilon+ifnull(val,0)+iv;
  224.  
  225.  
  226. -- 更新序号
  227. begin
  228. declare Vno int(12) default 0;
  229. declare Vid int(12) default null;
  230.  
  231. declare done int(10) default false;-- 遍历数据结束标志
  232. declare cur_no cursor for select id from rpt order by result desc;
  233. declare continue handler for not found set done = true;
  234.  
  235. open cur_no;
  236. no_loop:loop
  237. fetch cur_no into Vid;
  238. if done then
  239. leave no_loop;
  240. end if;
  241. set Vno=Vno+1;
  242. update rpt set no=Vno where id=Vid;
  243. end loop;
  244. close cur_no;
  245.  
  246. end;
  247.  
  248. select * from rpt order by no;
  249. truncate table rpt;
  250. end;
  251.  
  252. call yearchk2013(2013);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement