限定最近作爲最後一個先前數據爲股票:
查詢
select *
from historical_data hd
inner join
(
SELECT symbol_name, max(current_day) as nearest_day
from historical_data
where symbol_name IN ('WOCKPHARMA','YESBANK')
AND current_day <= DATE_SUB('2015-12-18', INTERVAL 6 DAY)
group by symbol_name
) nearest_data
on hd.symbol_name = nearest_data.symbol_name
and hd.current_day = nearest_data.nearest_day
;
輸出
+-------------+----------------------------+----------+----------+---------+-----------+----------+---------------+-------------+----------------------------+
| symbol_name | current_day | open_val | high_val | low_val | close_val | last_val | prevclose_val | symbol_name | nearest_day |
+-------------+----------------------------+----------+----------+---------+-----------+----------+---------------+-------------+----------------------------+
| WOCKPHARMA | December, 11 2015 00:00:00 | 1611 | 1620 | 1570.3 | 1581.25 | 1579 | 1602.1 | WOCKPHARMA | December, 11 2015 00:00:00 |
| YESBANK | December, 11 2015 00:00:00 | 709 | 713.7 | 672.25 | 680.6 | 683.45 | 707.1 | YESBANK | December, 11 2015 00:00:00 |
+-------------+----------------------------+----------+----------+---------+-----------+----------+---------------+-------------+----------------------------+
sqlfiddle
之前接近可能「最近」的意思是「最接近以前的」? – Bohemian
最近的當前日期值... – Pawan
@PreethiJain請檢查我的答案,如果給定的日期不匹配,請給出最近的以前的數據 – Thanga