SQLite:查询结果的摘要数据

时间:2016-06-14 13:31:43

标签: sqlite

我有以下查询,它为我提供了数据库中最近的10条记录:

SELECT
    dpDate AS Date,
    dpOpen AS Open,
    dpHigh AS High,
    dpLow AS Low,
    dpClose AS Close
FROM DailyPrices 
WHERE dpTicker = 'DL.AS'
ORDER BY dpDate DESC
LIMIT 10;

此查询的结果如下:

bash-3.2$ sqlite3 myData < Queries/dailyprice.sql
Date        Open        High        Low         Close     
----------  ----------  ----------  ----------  ----------
2016-06-13  4.0         4.009       3.885       3.933     
2016-06-10  4.23        4.236       4.05        4.08      
2016-06-09  4.375       4.43        4.221       4.231     
2016-06-08  4.406       4.474       4.322       4.35      
2016-06-07  4.377       4.466       4.369       4.384     
2016-06-06  4.327       4.437       4.321       4.353     
2016-06-03  4.34        4.428       4.316       4.335     
2016-06-02  4.434       4.51        4.403       4.446     
2016-06-01  4.51        4.512       4.317       4.399     
2016-05-31  4.613       4.67        4.502       4.526     
bash-3.2$

虽然我需要绘制提取的数据,但我还需要获取数据集的以下摘要数据:

  1. 最短日期==&gt; 2016年5月31日
  2. 最长日期==&gt; 2016年6月13日
  3. 最低日期的开放价值==&gt; 4.613
  4. 最大日期的关闭值==&gt; 3.933
  5. 最高柱数==&gt; 4.67
  6. 最低值列==&gt; 3.885
  7. 作为新手,我怎样才能解决这个问题?这可以在一个查询中完成吗?

    感谢您指出我正确的方向。

    致以最诚挚的问候,

    GAM

1 个答案:

答案 0 :(得分:0)

可以使用

实现所需的输出
  • 在方便的公用表表达式上聚合函数,
    它使用OPs表达式逐字记录
  • OPs方法,limit 1应用于公用表表达式,
    在十天之内获得思想和最大化

查询:

WITH Ten(Date,Open,High,Low,Close) AS 
(SELECT dpDate  AS Date,
        dpOpen  AS Open,
        dpHigh  AS High,
        dpLow   AS Low,
        dpClose AS Close
 FROM DailyPrices
 WHERE dpTicker = 'DL.AS'
 ORDER BY dpDate DESC LIMIT 10)

SELECT min(Date) AS mindate,
       max(Date) AS maxdate,
       (SELECT Open  FROM Ten ORDER BY Date ASC  LIMIT 1) AS Open,
       max(High) AS High,
       min(Low)  AS Low,
       (SELECT Close FROM Ten ORDER BY Date DESC LIMIT 1) AS Close
FROM Ten;

输出(.headers on.mode column):

mindate     maxdate     Open        High        Low         Close
----------  ----------  ----------  ----------  ----------  ----------
2016-05-31  2016-06-13  4.613       4.67        3.885       3.933

注意:
我认为OP最后一条评论中的值顺序与OP前面评论中的列顺序不匹配 我从前面的评论中选择了订单 在我看来,最后评论中的顺序是“注意,最大,开放,关闭,高,低” 将我提出的查询调整为该顺序很简单。

使用SQLite 3.18.0 2017-03-28 18:48:43

这是我的玩具数据库的.dump,即我的MCVE,以防有些事情不清楚。 (我没有输入多个小数位,它可能是一个浮点数。)

PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE dailyPrices (dpDate date, dpOpen float, dpHigh float, dpLow float, dpClose float, dpTicker varchar(10));
INSERT INTO dailyPrices(dpDate,dpOpen,dpHigh,dpLow,dpClose,dpTicker) VALUES('2016-06-13',4.0,4.009000000000000341,3.8849999999999997868,3.9329999999999998294,'DL.AS');
INSERT INTO dailyPrices(dpDate,dpOpen,dpHigh,dpLow,dpClose,dpTicker) VALUES('2016-06-10',4.2300000000000004263,4.2359999999999997655,4.0499999999999998223,4.080000000000000071,'DL.AS');
INSERT INTO dailyPrices(dpDate,dpOpen,dpHigh,dpLow,dpClose,dpTicker) VALUES('2016-06-09',4.375,4.4299999999999997157,4.2210000000000000852,4.2309999999999998721,'DL.AS');
INSERT INTO dailyPrices(dpDate,dpOpen,dpHigh,dpLow,dpClose,dpTicker) VALUES('2016-06-08',4.4059999999999996944,4.4740000000000001989,4.3220000000000000639,4.3499999999999996447,'DL.AS');
INSERT INTO dailyPrices(dpDate,dpOpen,dpHigh,dpLow,dpClose,dpTicker) VALUES('2016-06-07',4.3769999999999997797,4.4660000000000001918,4.3689999999999997726,4.384000000000000341,'DL.AS');
INSERT INTO dailyPrices(dpDate,dpOpen,dpHigh,dpLow,dpClose,dpTicker) VALUES('2016-06-06',4.3269999999999999573,4.4370000000000002771,4.3209999999999997299,4.3529999999999997584,'DL.AS');
INSERT INTO dailyPrices(dpDate,dpOpen,dpHigh,dpLow,dpClose,dpTicker) VALUES('2016-06-03',4.3399999999999998578,4.4370000000000002771,4.3209999999999997299,4.3529999999999997584,'DL.AS');
INSERT INTO dailyPrices(dpDate,dpOpen,dpHigh,dpLow,dpClose,dpTicker) VALUES('2016-06-02',4.4340000000000001634,4.5099999999999997868,4.4029999999999995807,4.4459999999999997299,'DL.AS');
INSERT INTO dailyPrices(dpDate,dpOpen,dpHigh,dpLow,dpClose,dpTicker) VALUES('2016-06-01',4.5099999999999997868,4.5119999999999995665,4.3170000000000001705,4.3990000000000000213,'DL.AS');
INSERT INTO dailyPrices(dpDate,dpOpen,dpHigh,dpLow,dpClose,dpTicker) VALUES('2016-05-31',4.6130000000000004334,4.6699999999999999289,4.5019999999999997797,4.525999999999999801,'DL.AS');
COMMIT;