如何消除此查询中的重复记录

时间:2015-12-18 10:56:09

标签: mysql

我有股票符号的历史数据,这是我的数据

CREATE TABLE IF NOT EXISTS `historical_data` (
  `symbol_name` varchar(70) DEFAULT NULL,
  `current_day` varchar(50) DEFAULT NULL,
  `open_val` varchar(20) DEFAULT NULL,
  `high_val` varchar(20) DEFAULT NULL,
  `low_val` varchar(20) DEFAULT NULL,
  `close_val` varchar(20) DEFAULT NULL,
  `last_val` varchar(20) DEFAULT NULL,
  `prevclose_val` varchar(20) DEFAULT NULL,
  UNIQUE KEY `symbol_name` (`symbol_name`,`current_day`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


INSERT INTO `historical_data` (`symbol_name`, `current_day`, `open_val`, `high_val`, `low_val`, `close_val`, `last_val`, `prevclose_val`) VALUES
    ('ZYDUSWELL', '13-DEC-2015', '814.75', '816.75', '784.25', '811.7', '811.50', '811.50'),
    ('ZYLOG', '13-DEC-2015', '2.9', '2.95', '2.85', '2.95', '2.95', '2.19'),
    ('ZYDUSWELL', '17-DEC-2015', '815.75', '815.75', '785.25', '810.7', '813.50', '813.50'),
    ('ZYLOG', '17-DEC-2015', '2.9', '2.95', '2.85', '2.95', '2.95', '2.19');

这就是我如何获得Top Looser的1周

SELECT *  FROM (SELECT distinct symbol_name , ROUND((prevclose_val-close_val),2) AS losers, STR_TO_DATE(current_day, '%d-%M-%Y'), 'weekly' AS `type` 
        FROM historical_data 
        WHERE date_format( STR_TO_DATE(current_day, '%d-%M-%Y'), '%Y%m%d')  >= date_format(date_sub(now(), interval 7 day), '%Y%m%d')
        ORDER BY losers ASC 
      ) AS sub_weekly;

我看到的结果是结果中的重复符号

请告诉我如何消除重复记录

这是我的sqlfiddle

http://sqlfiddle.com/#!9/e9c28/6

已编辑的部分

实际结果在使用GROUP BY之前工作正常

enter image description here

现在使用组后结果是

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您希望看到不同的符号,那么您可以按如下方式进行GROUP BY:

 SELECT *  FROM 
(SELECT distinct symbol_name , 
 ROUND((prevclose_val-close_val),2) AS losers, 
 STR_TO_DATE(current_day, '%d-%M-%Y'), 'weekly' AS `type` 
        FROM historical_data 
        WHERE date_format( STR_TO_DATE(current_day, '%d-%M-%Y'), '%Y%m%d')  >= date_format(date_sub(now(), interval 7 day), '%Y%m%d')
        GROUP BY symbol_name 
        ORDER BY losers ASC 
) AS sub_weekly;

结果集:

symbol_name losers  STR_TO_DATE(current_day, '%d-%M-%Y')    type
ZYLOG   -0.76   December, 13 2015 00:00:00  weekly
ZYDUSWELL   -0.2    December, 13 2015 00:00:00  weekly