2016-05-03 92 views
0

我保持股票價格在我的數據庫,這是我的數據庫結構如何計算股票的百分比變化值更改

CREATE TABLE `sector_wise` (
    `symbol_name` varchar(50) DEFAULT NULL, 
    `sector_name` varchar(50) DEFAULT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ; 

CREATE TABLE `historical_data` (
    `symbol_name` varchar(70) DEFAULT NULL, 
    `current_day` date DEFAULT NULL, 
    `open_val` decimal(15,2) DEFAULT NULL, 
    `high_val` decimal(15,2) DEFAULT NULL, 
    `low_val` decimal(15,2) DEFAULT NULL, 
    `close_val` decimal(15,2) DEFAULT NULL, 
    `last_val` decimal(15,2) DEFAULT NULL, 
    `prevclose_val` decimal(15,2) DEFAULT NULL, 
    `volume` varchar(30) DEFAULT 'NA' 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ; 

插入腳本:

Insert INTO sector_wise ('symbol_name','sector_name') VALUES ('VISESHINFO','Agriculture'); 



Insert INTO historical_data ('symbol_name','current_day','open_val','high_val','low_val','close_val','last_val','prevclose_val','volume') VALUES ('VISESHINFO','2016-05-03',0.05,0.10,0.05,0.05,0.05,0.10,45814.95); 

這是查詢我試過

SELECT sw.symbol_name AS symbol_name, 
     hd.current_day AS curr_day, 
     hd.open_val AS open_value, 
     hd.high_val AS high_value, 
     hd.low_val AS low_val, 
     hd.close_val AS close_val, 
     hd.prevclose_val AS prevclose_val, 
     (close_val-prevclose_val)/close_val*100 AS per_change 
FROM sector_wise sw, 
    historical_data hd 
WHERE sw.symbol_name = hd.symbol_name 
    AND sw.sector_name = 'Agriculture' 
    AND hd.current_day = 
    (SELECT max(current_day) 
    FROM historical_data) 
ORDER BY per_change; 

結果我得到後執行以上查詢是其實是錯誤的,這一比例chnage應該是-50.00,而不是-100.00

symbol_name curr_day  open_value high_value low_value close_val prevclose_val percentage_change 
VISESHINFO 2016-05-03  0.05   0.10   0.05   0.05   0.10     -100.000000 

你能告訴我到底哪兒錯就錯在計算百分比變化

+0

在哪裏,你是如何計算的價格變化? – datelligence

+0

在這一行(close_val-prevclose_val)/ close_val * 100 AS per_change – Pawan

回答

1

(close_val-prevclose_val)/close_val * 100將轉化爲如下:

((0.05-0.10)/0.05))*100 

其等於-100。你確定這個公式嗎?如果是價格變動,prevclose_val是不是應該用作公式中的分母?

+0

這是更多關於你的公式的問題,而不是你的sql腳本的問題。 – Lane

+0

ya,非常感謝,prevclose_val應該在公式中用作分母 – Pawan

0

我認爲,你只需要做(close_val/prev_close_val)* 100

0

有了這樣的: 關閉值= 0.05 Prev_close值= 0.10

Delta = 1-close_value/prev_close