在SELECT中选择

时间:2016-06-15 19:40:30

标签: sqlite

我有以下查询,我正在尝试重写:

SELECT 
    max(dpHigh) AS High
FROM DailyPrices
WHERE dpTicker = 'DL.AS' 
AND dpDate IN
(SELECT
    dpDate
 FROM DailyPrices
 WHERE dpTicker ='DL.AS'
 ORDER BY update DESC
 LIMIT 10);

查询给出了我所需的结果:

bash-3.2$ sqlite3 myData < Queries/high.sql
High      
----------
4.67      
bash-3.2$

由于在高值旁边,我希望扩展此查询以获得低值,最早的日期,最新日期等。出于这个原因,我正在尝试使用select in select语句重写等效查询。

SELECT 
    (SELECT         
        max(dpHigh)
    FROM DailyPrices
    WHERE dpTicker = 'DL.AS'
    AND dpDate IN 
        (SELECT dpDate
            FROM DailyPrices
            WHERE dpTicker ='DL.AS'
            ORDER BY dpDate DESC
            LIMIT 10) 
    )AS High
FROM DailyPrices
WHERE dpTicker = 'DL.AS';

执行查询吐出输出预期值,但是,它完全符合&#39; DL.AS&#39;的数据条目数。

...
4.67      
4.67      
4.67      
4.67      
4.67      
4.67      
4.67      
bash-3.2$

由于我是一名SQLite新手,我可能会忽略这一点。有人有什么建议吗?

BR

GAM

1 个答案:

答案 0 :(得分:1)

最外面的查询如下所示:

SELECT (...)
FROM DailyPrices
WHERE dpTicker = 'DL.AS';

这将为每个表行生成一个输出行,并匹配dpTicker

要生成单行,无论在某些表中找到多少行,都使用不带FROM的查询(已在子查询中处理过滤和聚合):

SELECT (...) AS High,
       (...) AS Low;