#まず削除
DROP PROCEDURE IF EXISTS myProc;
#作成
#日付をプロシジャーの引数に設定、戻り値はない
CREATE PROCEDURE myProc(IN aft_date DATETIME)
BEGIN
DECLARE done INT DEFAULT FALSE;#ループを抜けるフラグ
DECLARE dt DATETIME;#日付ごとにループ
DECLARE tdays CURSOR FOR SELECT aod as as_of_date FROM date_ots where aod > aft_date order by aod asc;#ループする日付たち
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;#日付が全部回ったら終了フラグ
OPEN tdays;#取得する
read_loop: LOOP#ループ開始
FETCH tdays INTO dt;
IF done THEN
LEAVE read_loop;#ループを抜ける
END IF;
insert into fund_correction_indices
select null,f.id,rt.aod,0,ci4.correction_value * rt.rate1 * rt.rate2 * rt.rate3 * rt.rate4,ci4.correction_value * rt.rate1 * rt.rate2 * rt.rate3 * rt.rate4,0
from
(
select dto.aod,f.fund_small_category_id,
avg(ci0.correction_value/ci1.correction_value) as rate1,
avg(ci1.correction_value/ci2.correction_value) as rate2,
avg(ci2.correction_value/ci3.correction_value) as rate3,
avg(ci3.correction_value/ci4.correction_value) as rate4,
avg(ci0.correction_value/ci4.correction_value) as rate_,
dto.4thd
from date_ots dto
inner join fund_correction_indices ci0 on ci0.as_of_date = dto.aod
inner join funds f on f.id = ci0.fund_id and f.kind not in ('平均','指数')
inner join fund_correction_indices ci1 on ci1.as_of_date = dto.1std and ci1.fund_id = ci0.fund_id
inner join fund_correction_indices ci2 on ci2.as_of_date = dto.2ndd and ci2.fund_id = ci0.fund_id
inner join fund_correction_indices ci3 on ci3.as_of_date = dto.3rdd and ci3.fund_id = ci0.fund_id
inner join fund_correction_indices ci4 on ci4.as_of_date = dto.4thd and ci4.fund_id = ci0.fund_id
where dto.aod = dt
and f.fund_small_category_id >=1 and f.fund_small_category_id <=57
group by fund_small_category_id,dto.aod
) rt
inner join funds f on f.fund_small_category_id = rt.fund_small_category_id and f.kind = '平均'
inner join fund_correction_indices ci4 on ci4.as_of_date = rt.4thd and ci4.fund_id = f.id
order by f.id,rt.aod
ON DUPLICATE KEY UPDATE
value=ci4.correction_value * rt.rate1 * rt.rate2 * rt.rate3 * rt.rate4,
correction_value=ci4.correction_value * rt.rate1 * rt.rate2 * rt.rate3 * rt.rate4;
END LOOP;
CLOSE tdays;
END
呼び出し
CALL myProc('2012-02-01')
もしCREATE PROCEDUREの時「Column count of mysql.proc is wrong」みたいなエラーが出たら
$ mysql_upgrade -uroot -p
rootパスワード入力
で直してあげてください。
0 件のコメント:
コメントを投稿