如何根据历史数据找到1天,1个月的最高输家

时间:2015-12-14 14:51:39

标签: mysql

我有以下表格结构来存储历史数据。

  CREATE TABLE `historical_data` (
      `symbol_name` varchar(70) DEFAULT NULL,
      `current_day` varchar(50) DEFAULT NULL,
      `open_val` varchar(50) ,
      `low_val` varchar(50) ,
     `close_val` varchar(50) ,
     `high_val` varchar(50) ,
      `prevclose_val` varchar(50) 
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;

这是我的样本数据

从目前的日期开始,我试图找出自当前日期起1周和1个月的最大失败者

我试过以下

select symbol_name, (prevclose_val-close_val) as losers  from historical_data 
order by losers asc limit 10

请告诉我如何才能在1周和1个月内完成这项工作。

样本数据将采用此格式

 Insert Into historical_data values('SBIN','14-DEC-2015','300.10','289.00','305.10','309.90','256.30');

sql fiddle

http://sqlfiddle.com/#!9/47794

1 个答案:

答案 0 :(得分:0)

你可以这样做*:

select * from (select symbol_name , (prevclose_val-close_val) as losers,'daily' as `type` from historical_data where current_day >= '2015-12-14' order by losers asc limit 10 ) as sub_daily
union
select * from (select symbol_name , (prevclose_val-close_val) as losers,'weekly' as `type` from historical_data where current_day >= '2015-12-7' order by losers asc limit 10 ) as sub_weekly
union
select * from (select symbol_name , (prevclose_val-close_val) as losers,'monthly' as `type` from historical_data where current_day >= '2015-11-14' order by losers asc limit 10 ) as sub_monthly;

顺便说一句,您应该将日期字段从“varchar”更改为“DATE”或“DATETIME”,并使用yyyy-mm-dd格式作为日期。这样你可以使用MySQL在你的约会上进行计算。

*此查询要求您的current_day字段为datedatetime类型。