我保持股票價格在我的數據庫,這是我的數據庫結構如何計算股票的百分比變化值更改
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
你能告訴我到底哪兒錯就錯在計算百分比變化
在哪裏,你是如何計算的價格變化? – datelligence
在這一行(close_val-prevclose_val)/ close_val * 100 AS per_change – Pawan