mysql存储过程 定时任务

#处理采集数据
DROP PROCEDURE IF EXISTS do_collect_price;
CREATE PROCEDURE do_collect_price()
BEGIN 
    DECLARE flag INT DEFAULT 0;#声明游标循环标识
    DECLARE now_time INT DEFAULT UNIX_TIMESTAMP();#声明游标循环标识
    DECLARE start_time int DEFAULT UNIX_TIMESTAMP(date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(day from now())-1 day),interval 1 month));
    DECLARE end_time int DEFAULT UNIX_TIMESTAMP(date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(day from now()) day),interval 0 month));
    DECLARE percent FLOAT DEFAULT 0.1;#去除最高最低价格百分比
    DECLARE date_time char(8) DEFAULT extract(year_month from NOW());#当前年份月份201310
    DECLARE num int DEFAULT 0;#总数
    DECLARE t_cut int DEFAULT 0;#需要去掉的价格一半
    DECLARE t_total int DEFAULT 0;#有效价格总数
    DECLARE temp_Price DECIMAL(18,4);
    DECLARE tMaterialID char(30);
    DECLARE tMaterialName char(250);
    DECLARE tMaterialUnit char(150);
    DECLARE tMaterialFormat char(150);

    DECLARE cur CURSOR FOR SELECT MaterialID,MaterialName,MaterialUnit,MaterialFormat,count(MaterialID) as num FROM collect_price WHERE add_time > start_time AND add_time < end_time GROUP BY MaterialID,MaterialName,MaterialUnit,MaterialFormat;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET flag=1;
    OPEN cur;
        #删除jcj_price当月价格包信息
        DELETE FROM jcj_price WHERE PriceMonth = date_time;
        #删除pricepacklist可用期间信息
        DELETE FROM pricepacklist WHERE packid = 1020 and packmonth = date_time;
        FETCH cur INTO tMaterialID,tMaterialName,tMaterialUnit,tMaterialFormat,num;
    WHILE flag <> 1 DO
        IF num < 3
            THEN
                select avg(MaterialPrice) from (select MaterialPrice from collect_price where add_time > start_time and add_time < end_time and MaterialID=tMaterialID and MaterialName=tMaterialName and MaterialUnit=tMaterialUnit and MaterialFormat=tMaterialFormat) p INTO temp_Price;
        ELSE
                SET t_cut = ROUND(num*percent);
                SET t_total = num - 2*t_cut;
                select avg(MaterialPrice) from (select MaterialPrice from collect_price where add_time > start_time and add_time < end_time and MaterialID=tMaterialID and MaterialName=tMaterialName and MaterialUnit=tMaterialUnit and MaterialFormat=tMaterialFormat order by MaterialPrice asc limit t_cut,t_total) p INTO temp_Price;
        END IF;

        insert into  jcj_price(MaterialID,PriceMonth,Price,MaterialName,MaterialUnit,MaterialFormat)values(tMaterialID,date_time,temp_Price,tMaterialName,tMaterialUnit,tMaterialFormat);

        FETCH cur INTO tMaterialID,tMaterialName,tMaterialUnit,tMaterialFormat,num;
    END WHILE;
    insert into pricepacklist(packid,packmonth,packver,packname,periodtype,enabled)values(1020,date_time,1,'',1,0);
    #备份收集数据
    #SELECT * INTO collect_price_bakup FROM collect_price WHERE add_time > start_time and add_time < end_time;
    #DELETE FROM collect_price WHERE add_time > start_time and add_time < end_time;
    CLOSE cur;
END

#call pricepack.do_collect_price()

转载自:http://www.9958.pw/post/mysql_procedure

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值