【问题标题】:Need store procedure to calculate from these two tables需要存储过程从这两个表中计算
【发布时间】:2018-11-03 07:33:50
【问题描述】:

我应该在 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` 

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

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

【问题讨论】:

  • 其他用户将您的问题标记为低质量和需要改进。我重新措辞/格式化您的输入,使其更容易阅读/理解。请查看我的更改以确保它们反映您的意图。如果您对我有其他问题或反馈,请随时给我留言。
  • 我试图解决你的问题,但老实说:我放弃了。绝对不清楚这里应该如何将事物组合在一起。两件事情。 A) 您希望其他人利用空闲时间来帮助解决您的问题,因此请使用该预览窗口和 edit 帮助信息来编写一个人类可读的问题。 B)最好包括你自己解决问题的努力,不要仅仅因为你在这里倾倒了你的要求而期望别人来做你的工作(正如所说的:你倾倒它们的方式没有激励任何人帮助你)。跨度>
  • 如果您有使用过的链接,请将其包含在您的问题中。除此之外,我告诉你的问题有什么问题。将链接放入 cmets ... 对您输入的糟糕格式没有帮助。
  • 不,我已经格式化并给出了我的问题,而且我是初学者,所以我正在尝试扩展,但我无法破解它。所以请帮助我

标签: sql stored-procedures


【解决方案1】:

我已经破解了它,它现在工作正常。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2012-02-25
    相关资源
    最近更新 更多