如何在 MySQL 存储过程中计算这些日期差异和乘法?
How can I calculate these date difference,multiplication in a MySQL stored procedure?
我正在尝试在 mysql 内实施一个程序,该程序 returns 基于计算的天数。购买日期(added_date)传入,然后计算完成。应该相对简单,但我似乎无法让它发挥作用。我不确定这是否适合 "select statement",如果这非常简单,我深表歉意,但这是我的第一个 运行 程序。
如果有人能告诉我应该怎么做,将不胜感激。
谢谢!
我有一个名为“calculation”(TABLE 3) 的 table,它包含当前正在使用的字段 "elap_yend " (int)在调用存储过程之前为空。
Table 1:
cid parent_cat category_name category_life
22 0 Office Equipment-M' 1080
23 0 Office Equipment-O' 1800
24 0 F & F' 3600
25 0 Staff assets', 1800
27 0 Motor vehicle', 2880
28 0 Air conditioner' 5400
29 0 Land & Building', 2160
30 0 Temporary Partition 365
31 0 Electrical Fittings 3600
32 0 Generator' 5400
33 0 Software' 1800
34 0 Computer-N' 2160
35 24 chair' 3600
表 2:
pid cid product_name product_price added_date cgst sgst igst total depre
60 22 RHFL\test[=12=]1 20000 2018-11-02 1800 1800 0 23600 o
61 27 RHFL\test[=12=]2 13500 2018-11-02 2345 2345 0 15930 12
62 29 RHFL\test[=12=]3 65000 2018-11-02 2345 2345 0 76700 12
63 31 RHFL\test[=12=]4 10000 2018-11-02 2345 2345 0 1180 12
64 24 RHFL\test[=12=]5 10000 2018-11-02 2345 2345 0 11800 1
65 24 RHFL\test[=12=]6 13500 2018-11-02 2345 2345 0 15930 12
66 34 RHFL\test[=12=]7 13500 2018-11-02 2345 2345 0 15930 12
67 22 RHFL\test[=12=]8 65004 2018-11-02 2345 2345 0 76704 12
68 25 RHFL\test[=12=]9 10000 0000-00-00 2345 2345 0 11800 12
69 22 RHFL\test0 65000 0000-00-00 2345 2345 0 76700 10
70
Table 3: 存储过程的输出应该反映在下面 table:
end_date elap_yend rem_days depre_cur cur_wdv depre_next next_wdv acc_depre
2018-11-02 0 0 0 0 0 0
输出计算:
以上是带零的示例输出。这不是确切的输出,但每当我调用存储过程时,它都应该进行以下计算。
DELIMITER $$
CREATE PROCEDURE calculationTemp(
in till_yend date,
)
BEGIN
DECLARE till_yend date;
SELECT datediff(now(),added_date) INTO till_yend
FROM products
IF till_yend > 0 THEN
SET till_yend = select datediff(now(),added_date) from products;
ELSEIF till_yend < 0 THEN
SET till_yend = 0;
END IF;
insert into calculationTemp
END$$
计算工作:
过去的年末:
elapsed_yend = added_date(来自产品 table(No:2))直到每年 31/March/xxxx.
这两个日期之间的天数
remaining_days = category_life -(added_date(从产品(编号:1))到日期之间的日期差异)
category_life(来自类别table (No:1))Ex:mobile it's life will be 1080 days.
本年度折旧:
depreciation_cur = (depre/category_life)*elapsed_yend
当前年份记下值:
current_wdv = 贬值 - depreciation_cur
明年折旧:
depreciation_next = (depre / category_life) * D
D = 每年 01/April/xxxx 到 end_date 之间的天数差异(根据计算 table)
下一年记下的值:
next_wdv = current_wdv - depreciation_next
累计折旧:
accumulate_depre = depreciation_cur + depreciation_next
在此处发布我的答案,以了解有关改进我的商店程序的任何想法。谢谢!!!
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
我正在尝试在 mysql 内实施一个程序,该程序 returns 基于计算的天数。购买日期(added_date)传入,然后计算完成。应该相对简单,但我似乎无法让它发挥作用。我不确定这是否适合 "select statement",如果这非常简单,我深表歉意,但这是我的第一个 运行 程序。
如果有人能告诉我应该怎么做,将不胜感激。
谢谢!
我有一个名为“calculation”(TABLE 3) 的 table,它包含当前正在使用的字段 "elap_yend " (int)在调用存储过程之前为空。
Table 1:
cid parent_cat category_name category_life
22 0 Office Equipment-M' 1080
23 0 Office Equipment-O' 1800
24 0 F & F' 3600
25 0 Staff assets', 1800
27 0 Motor vehicle', 2880
28 0 Air conditioner' 5400
29 0 Land & Building', 2160
30 0 Temporary Partition 365
31 0 Electrical Fittings 3600
32 0 Generator' 5400
33 0 Software' 1800
34 0 Computer-N' 2160
35 24 chair' 3600
表 2:
pid cid product_name product_price added_date cgst sgst igst total depre
60 22 RHFL\test[=12=]1 20000 2018-11-02 1800 1800 0 23600 o
61 27 RHFL\test[=12=]2 13500 2018-11-02 2345 2345 0 15930 12
62 29 RHFL\test[=12=]3 65000 2018-11-02 2345 2345 0 76700 12
63 31 RHFL\test[=12=]4 10000 2018-11-02 2345 2345 0 1180 12
64 24 RHFL\test[=12=]5 10000 2018-11-02 2345 2345 0 11800 1
65 24 RHFL\test[=12=]6 13500 2018-11-02 2345 2345 0 15930 12
66 34 RHFL\test[=12=]7 13500 2018-11-02 2345 2345 0 15930 12
67 22 RHFL\test[=12=]8 65004 2018-11-02 2345 2345 0 76704 12
68 25 RHFL\test[=12=]9 10000 0000-00-00 2345 2345 0 11800 12
69 22 RHFL\test0 65000 0000-00-00 2345 2345 0 76700 10
70
Table 3: 存储过程的输出应该反映在下面 table:
end_date elap_yend rem_days depre_cur cur_wdv depre_next next_wdv acc_depre
2018-11-02 0 0 0 0 0 0
输出计算:
以上是带零的示例输出。这不是确切的输出,但每当我调用存储过程时,它都应该进行以下计算。
DELIMITER $$
CREATE PROCEDURE calculationTemp(
in till_yend date,
)
BEGIN
DECLARE till_yend date;
SELECT datediff(now(),added_date) INTO till_yend
FROM products
IF till_yend > 0 THEN
SET till_yend = select datediff(now(),added_date) from products;
ELSEIF till_yend < 0 THEN
SET till_yend = 0;
END IF;
insert into calculationTemp
END$$
计算工作:
过去的年末: elapsed_yend = added_date(来自产品 table(No:2))直到每年 31/March/xxxx.
这两个日期之间的天数
remaining_days = category_life -(added_date(从产品(编号:1))到日期之间的日期差异)
category_life(来自类别table (No:1))Ex:mobile it's life will be 1080 days.
本年度折旧: depreciation_cur = (depre/category_life)*elapsed_yend
当前年份记下值:
current_wdv = 贬值 - depreciation_cur
明年折旧: depreciation_next = (depre / category_life) * D D = 每年 01/April/xxxx 到 end_date 之间的天数差异(根据计算 table)
下一年记下的值:
next_wdv = current_wdv - depreciation_next
累计折旧:
accumulate_depre = depreciation_cur + depreciation_next
在此处发布我的答案,以了解有关改进我的商店程序的任何想法。谢谢!!!
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