2015-12-21 63 views
0

这是我的数据结构是否可以使用唯一的查询来获得所需的结果?

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 
); 



INSERT INTO `historical_data` (`symbol_name`, `current_day`, `open_val`, `high_val`, `low_val`, `close_val`, `last_val`, `prevclose_val`) VALUES 
    ('WOCKPHARMA', '2015-12-11', 1611.00, 1620.00, 1570.30, 1581.25, 1579.00, 1602.10), 
    ('YESBANK', '2015-12-11', 709.00, 713.70, 672.25, 680.60, 683.45, 707.10), 
    ('WOCKPHARMA', '2015-12-14', 1572.50, 1584.70, 1545.00, 1559.55, 1557.60, 1581.25), 
    ('YESBANK', '2015-12-14', 679.10, 689.00, 668.00, 683.25, 683.65, 680.60), 
    ('WOCKPHARMA', '2015-12-15', 1564.70, 1580.50, 1558.00, 1572.10, 1567.50, 1559.55), 
    ('YESBANK', '2015-12-15', 688.00, 694.20, 675.75, 691.35, 688.25, 683.25), 
    ('WOCKPHARMA', '2015-12-16', 1581.50, 1617.90, 1578.00, 1587.15, 1589.00, 1572.10), 
    ('YESBANK', '2015-12-16', 697.00, 710.60, 694.25, 698.55, 699.15, 691.35), 
    ('WOCKPHARMA', '2015-12-17', 1596.10, 1642.00, 1576.05, 1628.20, 1636.80, 1587.15), 
    ('YESBANK', '2015-12-17', 708.00, 723.75, 705.70, 721.10, 720.00, 698.55), 
    ('WOCKPHARMA', '2015-12-18', 1630.00, 1654.85, 1620.30, 1627.55, 1631.00, 1628.20), 
    ('YESBANK', '2015-12-18', 717.90, 727.45, 713.60, 718.70, 720.20, 721.10); 

,并找出基于当前数据目前我在做什么使用两个查询的涨幅居前

第一个查询Fethes我的数据库中存在的最后日期

select current_day from historical_data order by current_day desc limit 1 

而在第二个查询我使用获得的最后日期从第一查询

select symbol_name , current_day ,close_val,prevclose_val, (close_val-prevclose_val) as toploosers from historical_data where current_day = ? order by toploosers desc limit 10 

这是我sqlfiddle,

可否请你让我知道如何实现这一目标 http://sqlfiddle.com/#!9/3693b

回答

1
SELECT symbol_name, 
     current_day, 
     close_val, 
     prevclose_val, 
     (close_val-prevclose_val) AS toploosers 
FROM historical_data 
WHERE current_day = 
    (SELECT max(current_day) 
    FROM historical_data) 
ORDER BY toploosers DESC LIMIT 10 
+0

更好的表现还有 –

+0

没有格式化....没有评论....没有提及的逻辑.....下次更多_elaborate _ – NoobEditor

0

如何使用子查询

SELECT symbol_name, 
     current_day, 
     close_val, 
     prevclose_val, 
     (close_val-prevclose_val) AS toploosers 
FROM historical_data 
WHERE current_day **IN 
    (SELECT current_day 
    FROM historical_data 
    ORDER BY current_day DESC LIMIT 1)** 
ORDER BY toploosers DESC LIMIT 10 
+0

谢谢,但是当我运行查询我得到foloowing erorr“此版本的MySQL还不支持'限制和IN /所有/任何/某些子查询' – Pawan

+0

您可以尝试使用=而不是在 –

+0

没有格式。 ...没有评论....没有提及逻辑.....请下次更多_elaborate_! – NoobEditor

1

选择最大使用子查询的日期如下

select symbol_name , current_day ,close_val,prevclose_val, (close_val-prevclose_val) as toploosers from historical_data where current_day = (select max(current_day) from historical_data) order by toploosers desc limit 10 
相关问题