mysql - Calculate Percentage from two different table -
i have 2 tables, , want percentage value , update percentage table. suppose have 2 table this,
table 1
date open high low close stock_id 2013-01-02 10 20 5 15 1 2013-01-02 150 200 100 170 2 2013-01-03 15 30 10 20 1
table 2
date p_high p_low percent stock_id 2013-01-02 25 10 0.00 1 2013-01-02 210 120 0.00 2 2013-01-03 40 20 0.00 1
i want calculate percentage using formula
(p_high - high) / high
percentage stock_id = 1
in date = 2013-01-02
this.
(25 - 20) / 20 = 25.00
when percentage, want update table 2 this,
date p_high p_low percent stock_id 2013-01-02 25 10 25.00 1
how can in mysql?
thank help.
ddl
create table table1( created_dt date, open decimal(5,2), high decimal(5,2), low decimal(5,2), close decimal(5,2), stock_id integer ); create table table2( created_dt date, p_high decimal(5,2), p_low decimal(5,2), percent decimal(5,2), stock_id integer );
dml
insert table1 values ('2013-01-02',10,20,5,15,1); insert table1 values ('2013-01-02',150,200,100,170,2); insert table1 values ('2013-01-03',15,30,10,20,1); insert table2 values('2013-01-02',25,10,0.00,1); insert table2 values('2013-01-02',210,120,0.00,2); insert table2 values('2013-01-02',40,20,0.00,1); update table2 join table1 on table1.created_dt = table2.created_dt , table1.stock_id = table2.stock_id set percent = ((p_high - high)/high);
working example http://sqlfiddle.com/#!2/21d02/1/0
Comments
Post a Comment