且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在MySQL存储过程中计算这些日期差和乘法?

更新时间:2022-12-09 19:31:54

我的答案在这里发布,以获取有关改善我的商店流程的任何想法.谢谢!!!

Got My answer posting here to get any idea regarding to improve my store procedure. Thank You!!!

 BEGIN
    DECLARE cur_depre,cur_wtvalue,next_depre,next_wtvalue,accum_depre,pro_loss double(20,2);
    DECLARE days,next_diff int;

    CREATE TABLE IF NOT EXISTS calc AS (SELECT p.pid,p_date,days,next_diff,cur_depre,cur_wtvalue,next_depre,next_wtvalue,accum_depre,pro_loss,c.cid,p.added_date,c.category_life,p.depre,p.sale_status,p.sale_date,p.sale_amount from products p,products d,categories c WHERE p.pid=d.pid AND p.cid=c.cid 
     );
     INSERT INTO calc (PID) 
    SELECT PID FROM products WHERE PID NOT IN (SELECT PID FROM calc);
    UPDATE calc set days = datediff(p_date,added_date);
    UPDATE calc set days = 0 WHERE datediff(p_date,added_date) < 0;
    UPDATE calc set next_diff = datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) );
    UPDATE calc set next_diff = datediff('2019-03-31',added_date) WHERE added_date>p_date;
    UPDATE calc set cur_depre = (depre/category_life)*datediff(p_date,added_date);

    UPDATE calc set cur_depre = 0 where (depre/category_life)*datediff(p_date,added_date)<0;

    UPDATE calc set cur_wtvalue =(depre-(depre/category_life)*datediff(p_date,added_date)); 

    UPDATE calc set cur_wtvalue = 0 WHERE added_date>p_date;

    UPDATE calc set next_depre =( (depre/category_life)*datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) )) where added_date < p_date;

    UPDATE calc set next_depre =( (depre/category_life) * datediff('2019-03-31',added_date)) WHERE added_Date > p_date;

    UPDATE calc SET next_wtvalue = depre -(depre/category_life)*datediff(p_date,added_date) - (( (depre/category_life)*datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) )) );
    UPDATE calc SET next_wtvalue = depre - (( (depre/category_life) * datediff('2019-03-31',added_date)))  WHERE added_date>p_date;

    UPDATE calc SET accum_depre = ((depre/category_life)*datediff(p_date,added_date))+( (depre/category_life)*datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) )) ;
    UPDATE calc SET accum_depre =( (depre/category_life) * datediff('2019-03-31',added_date))  WHERE added_Date > p_date;

    UPDATE calc SET pro_loss = sale_amount-(depre -(depre/category_life)*datediff(p_date,added_date) - (( (depre/category_life)*datediff(sale_date,DATE_ADD(p_date, INTERVAL 1 DAY) )) ) ); 

    UPDATE calc SET pro_loss = 0 WHERE sale_status=0;
    SELECT * from calc;

    END