需要存储过程才能从这两个表中进行计算

时间:2018-11-03 07:33:50

标签: php stored-procedures xampp

我应该在xampp中执行此操作,为此需要一个sp结构,因为如果可能,我是一个初学者,可以通过详细说明为该方案提供完整的sp。诸如crud操作之类的其他结构已使用php,javascript

完成
1. SELECT `cid`, `parent_cat`, `category_name`, `category_life`, `status` FROM `categories`
2. SELECT `pid`, `cid`, `bid`, `product_name`, `product_price`, `product_stock`, `added_date`, `p_status`, `loc`, `in_no`, `gst_no`, `cgst`, `sgst`, `igst`, `total`, `depre`, `pur_from` FROM `products` 

从上面两个表的i中,我应该获取记录并计算这些字段并插入下表中:

3. SELECT `elapsed_yend`, `remaining_days`, `depreciation_cur`, `current_wdv`, `depreciation_next`, `next_wdv`, `accumulate_depre`, `sale_amount`, `pro_los`, `end_date` FROM `calculation` 

例如,这些计算应在产品表中进行:

CGST:
    cgst = product_price * 09
    sgst = product_price * 09
    igst = product_price * 09

if status is 1 it should calculate  igst other cgst,sgst must be 0(zero)
             0 it should calculate  cgst,sgst other igst  must be 0(zero)

        Depreciation Amount:

            depre = product_price + .5(cgst+sgst+igst)


Elapsede year end :
        elapsed_yend = added_date(from products table (No:2)) till every year 31/March/xxxx.

        Number of days between these two dates

    remaining_days = category_life - (date difference between added_date(from products (No:1)) till date)

    category_life(from categories table (No:1))Ex:mobile it's life will be 1080 days.

    Current Year depreciation:
        depreciation_cur  = (depre/category_life)*elapsed_yend

    Current Year Written down value:

        current_wdv  = depre - depreciation_cur

    Next Year depreciation :

        depreciation_next = (depre / category_life) * D
        D = days difference between every year 01/April/xxxx till end_date(from calculation table)

    Next Year Written down value:   

        next_wdv = current_wdv - depreciation_next

    Accumulate Depreciation :

        accumulate_depre = depreciation_cur + depreciation_next

1 个答案:

答案 0 :(得分:0)

如果需要,请提出一些建议以改进我的存储过程。无论如何,我已经破解了它,到目前为止它运行良好。非常感谢所有支持,尤其是堆栈溢出团队

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