2016-06-14 64 views
1

我有以下查詢爲我提供了最新的10條記錄在數據庫中:SQLite的:一個查詢結果的彙總數據

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. 最小日期==> 2016年5月31日
  2. 最大日期==> 2016年6月13日
  3. 在最小日期==>打開值4.613
  4. 在最大日期==>關閉值3.933
  5. 最大高柱==> 4.67
  6. 最低低列==> 3.885

作爲新手,我該如何解決這個問題?這可以在一個查詢中完成嗎?

感謝您指點我正確的方向。

此致

GAM

+1

直接與查詢可能會非常棘手。是否有任何限制使用PL/SQL或任何其他語言來解析它 – DevD

+0

親愛的DevD,我使用Perl來讀取數據到數組中。隨後,我可以使用Perl獲取想要的值。但是,我試圖探索SQLite獲取數據的方式 - 我想爬上SQLite學習曲線。作爲新手,我一直在探索查詢結果的途徑 - 但在這個問題上沒有成功。 BR GAM –

+0

單個查詢返回具有固定列數和特定行數的'表'。請顯示您的查詢的輸出結果應該如何。 –

回答

0

所需的輸出可以與

  • 聚集函數上一個方便的公用表表達式來實現,
    其使用的OP表達逐字
  • OP的方法,limit 1適用於通用表格表格,
    用於取得m十天中銦酸和的maxDate

查詢:

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在前面的評論中的列順序不匹配。
我選擇了前面評論的順序。
上次評論中的順序在我看來是「mindate,maxdate,Open,Close,High,Low」。
將我建議的查詢調整爲該順序很簡單。

使用SQLite 3.18.0 2017年3月28日18點48分43秒

這裏是我的玩具數據庫,即我MCVE的.dump,在一些情況下,目前還不清楚。 (我沒有輸入許多小數位,它可能是一個浮點四捨五入的東西。)

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;