使用多个聚合函数查找每组的最后一条记录-SQLite

时间:2019-03-12 21:45:53

标签: sql sqlite group-by aggregate-functions greatest-n-per-group

我有一个表("trds"),其中包含字段RefDate, Symbol, Timestamp, Sequence, Quantity, Price, SaleCondition, Pid,Submkt"select * from trds limit 100"):

    RefDate Symbol Timestamp Sequence Quantity  Price SaleCondition Pid SubMkt
1   2015-08-03   AAPL  14400002      507       10 121.50          @ TI   P       
2   2015-08-03   AAPL  14400026     1017      100 121.39             T   P       
3   2015-08-03   AAPL  14400026     1018      520 121.39             T   P       
4   2015-08-03   AAPL  14400026     1019      100 121.40             T   P       
5   2015-08-03   AAPL  14400028     1020      100 121.49             T   P       
6   2015-08-03   AAPL  14400028     1021     2172 121.49             T   P       
7   2015-08-03   AAPL  14425890     1026      100 121.49             T   P       
8   2015-08-03   AAPL  14426989     1027      100 121.49             T   P       
9   2015-08-03   AAPL  14433658     1028      137 121.40          @FT    P       
10  2015-08-03   AAPL  14628998     1029      200 121.40             T   P       
11  2015-08-03   AAPL  14637576     1030     5328 121.40          @ T    P       
12  2015-08-03   AAPL  14637576     1031      200 121.45          @FT    Q       
13  2015-08-03   AAPL  15116858     1032     3040 121.40             T   P       
14  2015-08-03   AAPL  15129926     1034     1295 121.40             T   P       
15  2015-08-03   AAPL  15130128     1035      201 121.30             T   P       
16  2015-08-03   AAPL  15237959     1036      126 121.30             T   P       
17  2015-08-03   AAPL  15553155     1043       20 121.03          @ TI   P       
18  2015-08-03   AAPL  15646925     1044       80 121.03          @ TI   P       
19  2015-08-03   AAPL  15844129     1050       40 121.22          @FTI   Q       
20  2015-08-03   AAPL  16908849     1055      190 121.22          @FT    Q       
21  2015-08-03   AAPL  16998884     1056      260 121.22          @FT    Q       
22  2015-08-03   AAPL  17682717     1057       10 121.22          @FTI   Q       
23  2015-08-03   AAPL  17682717     1058      190 121.23          @FT    Q       
24  2015-08-03   AAPL  17736566     1059      100 121.23          @FT    Q       
25  2015-08-03   AAPL  18102786     1061      100 121.22          @FT    Q       
26  2015-08-03   AAPL  18102786     1062      100 121.22          @FT    Q       
27  2015-08-03   AAPL  18103386     1063      700 121.23          @FT    Q       
28  2015-08-03   AAPL  19170799     1064      200 121.22          @FT    P       
29  2015-08-03   AAPL  19171001     1065       40 121.22          @FTI   P       
30  2015-08-03   AAPL  19557088     1068      500 121.21             T   P       
31  2015-08-03   AAPL  19995780     1072       50 121.00          @FTI   P       
32  2015-08-03   AAPL  19995780     1073      450 121.00          @FT    P       
33  2015-08-03   AAPL  20110219     1074      100 121.00          @FT    P       
34  2015-08-03   AAPL  20131347     1075      100 121.00          @FT    P       
35  2015-08-03   AAPL  20288087     1076       50 121.01          @FTI   P       
36  2015-08-03   AAPL  20288282     1077      250 121.00          @FT    P       
37  2015-08-03   AAPL  20776851     1081      200 121.22             T   P       
38  2015-08-03   AAPL  20777052     1082      300 121.22             T   P       
39  2015-08-03   AAPL  21416707     1084      200 121.22             T   P       
40  2015-08-03   AAPL  21416907     1085      300 121.22             T   P       
41  2015-08-03   AAPL  21740935     1086      200 121.22          @FT    P       
42  2015-08-03   AAPL  21741136     1087       33 121.22          @FTI   P       
43  2015-08-03   AAPL  22289290     1088       50 121.22          @ TI   P       
44  2015-08-03   AAPL  22302889     1089      150 121.22             T   P       
45  2015-08-03   AAPL  22303093     1090      350 121.22             T   P       
46  2015-08-03   AAPL  22324450     1091      100 121.22             T   P       
47  2015-08-03   AAPL  22325298     1092      100 121.22             T   P       
48  2015-08-03   AAPL  22489509     1093      200 121.22             T   P       
49  2015-08-03   AAPL  22489711     1094      300 121.22             T   P       
50  2015-08-03   AAPL  22651416     1096      200 121.22          @FT    P       
51  2015-08-03   AAPL  22656656     1097       77 121.22          @ TI   P       
52  2015-08-03   AAPL  22656857     1098       10 121.23          @ TI   P       
53  2015-08-03   AAPL  22762084     1099       87 121.37          @FTI   Q       
54  2015-08-03   AAPL  22762084     1100      413 121.38          @FT    Q       
55  2015-08-03   AAPL  22956526     1103       20 121.20          @ TI   P       
56  2015-08-03   AAPL  22956727     1104      180 121.18             T   P       
57  2015-08-03   AAPL  23254802     1105       36 121.27          @ TI   P       
58  2015-08-03   AAPL  24077374     1110      144 121.27             T   P       
59  2015-08-03   AAPL  24077374     1111       50 121.29          @ TI   P       
60  2015-08-03   AAPL  24077374     1112      306 121.30             T   P       
61  2015-08-03   AAPL  24121832     1113        2 121.30          @ TI   P       
62  2015-08-03   AAPL  24164641     1116     1000 121.30             T   P       
63  2015-08-03   AAPL  24168414     1117      600 121.30             T   P       
64  2015-08-03   AAPL  24192881     1120       92 121.30          @ TI   P       
65  2015-08-03   AAPL  24192882     1121      408 121.38          @FT    Q       
66  2015-08-03   AAPL  24331392     1122      400 121.45          @ T    P       
67  2015-08-03   AAPL  24331392     1123      100 121.38          @FT    Q       
68  2015-08-03   AAPL  24336664     1124       79 121.38          @ TI   P       
69  2015-08-03   AAPL  24464675     1125     1000 121.40             T   P       
70  2015-08-03   AAPL  24587817     1126      496 121.38             T   P       
71  2015-08-03   AAPL  24588021     1127      504 121.39             T   P       
72  2015-08-03   AAPL  24600057     1128      500 121.38             T   P       
73  2015-08-03   AAPL  24667891     1130      500 121.37             T   P       
74  2015-08-03   AAPL  24723357     1131       50 121.39          @ TI   P       
75  2015-08-03   AAPL  24778654     1132     1000 121.39             T   P       
76  2015-08-03   AAPL  24908008     1133      200 121.39             T   P       
77  2015-08-03   AAPL  24908210     1134      300 121.40             T   P       
78  2015-08-03   AAPL  24912914     1135     1500 121.40             T   P       
79  2015-08-03   AAPL  25011487     1136      500 121.40             T   P       
80  2015-08-03   AAPL  25018982     1137      100 121.40             T   P       
81  2015-08-03   AAPL  25023375     1138       21 121.40          @ TI   P       
82  2015-08-03   AAPL  25064872     1139      600 121.40             T   P       
83  2015-08-03   AAPL  25111247     1140      500 121.40             T   P       
84  2015-08-03   AAPL  25112575     1141      200 121.40             T   P       
85  2015-08-03   AAPL  25139881     1142      200 121.40             T   P       
86  2015-08-03   AAPL  25140659     1143       79 121.40          @ TI   P       
87  2015-08-03   AAPL  25140869     1144      421 121.45             T   P       
88  2015-08-03   AAPL  25219916     1146      200 121.45             T   P       
89  2015-08-03   AAPL  25229789     1147       50 121.36          @FTI   P       
90  2015-08-03   AAPL  25229988     1148       29 121.35          @FTI   P       
91  2015-08-03   AAPL  25290394     1160      200 121.43             T   P       
92  2015-08-03   AAPL  25392283     1168       30 121.40          @ TI   P       
93  2015-08-03   AAPL  25421012     1169      300 121.40             T   P       
94  2015-08-03   AAPL  25755052     1173        2 121.45          @ TI   P       
95  2015-08-03   AAPL  25763189     1174       60 121.44          @ TI   P       
96  2015-08-03   AAPL  25942067     1182      200 121.46          @FT    P       
97  2015-08-03   AAPL  25942068     1183      100 121.47          @FT    P       
98  2015-08-03   AAPL  25942068     1184      100 121.47          @FT    P       
99  2015-08-03   AAPL  25942070     1185      100 121.47          @FT    P       
100 2015-08-03   AAPL  25948942     1186     1000 121.48             T   

我需要创建一个查询,以每隔五分钟将所有交易分组在一起,并找到每组的最低/最高/平均价格和最后价格"Timestamp"是午夜以来的毫秒数(所以9:30是34200000),我只想在9:30到4:00之间添加。我用五分钟的间隔(300,000毫秒)除以timebucket来创建cast(timestamp/300e3) as int) as timebucket

最终结果应如下所示:enter image description here

我可以生成代码以获取最低/最高/平均价格,如下所示:

dbGetQuery(nqdb, statement = "select t.RefDate, t.Symbol, cast(t.Timestamp/300e3 as int) as timeBucket, 
    Min(time(t.timestamp/1000, 'unixepoch')) as startTime, Max(t.Price) as MaxPrice,
    Min(t.Price) as MinPrice, Avg(t.Price) as AvgPrice
    from trds t
    where (t.Timestamp between 34200000 and 57600000) and
    SaleCondition not glob '*[CGIHMNPQRTUVWZ47]*' group by timeBucket, symbol order by symbol ASC")

这有点接近,尽管StartTime在HH:MM:SS字段中有不应该的秒数,并且我已经尝试了从子查询到子选择的所有内容,并通过“ greatest- n-per-group”论坛以获取“ LastPrice”无效。输出如下:

   RefDate Symbol timeBucket startTime MaxPrice MinPrice  AvgPrice
1   2015-08-03   AAPL        114  09:30:00 121.5000 121.0500 121.22574
2   2015-08-03   AAPL        115  09:35:00 122.4700 121.2200 121.86515
3   2015-08-03   AAPL        116  09:40:00 122.5700 122.1000 122.37850
4   2015-08-03   AAPL        117  09:45:00 122.2000 121.8901 122.00520
5   2015-08-03   AAPL        118  09:50:00 122.2900 122.0000 122.10803
6   2015-08-03   AAPL        119  09:55:00 122.2100 121.7000 122.02053
7   2015-08-03   AAPL        120  10:00:00 121.8900 121.4100 121.73007
8   2015-08-03   AAPL        121  10:05:00 121.5500 121.2500 121.38513
9   2015-08-03   AAPL        122  10:10:00 121.6200 121.2550 121.48453
10  2015-08-03   AAPL        123  10:15:00 121.5200 121.2700 121.38975
11  2015-08-03   AAPL        124  10:20:00 121.3400 121.1500 121.25174
12  2015-08-03   AAPL        125  10:25:00 121.1600 120.7750 120.92567
13  2015-08-03   AAPL        126  10:30:00 121.0400 120.8100 120.90262
14  2015-08-03   AAPL        127  10:35:01 121.0000 120.7800 120.88182
15  2015-08-03   AAPL        128  10:40:00 121.0000 120.8600 120.91147
16  2015-08-03   AAPL        129  10:45:00 120.9083 120.7330 120.81935
17  2015-08-03   AAPL        130  10:50:00 120.8400 120.5500 120.71769
18  2015-08-03   AAPL        131  10:55:00 120.7200 120.5300 120.62324
19  2015-08-03   AAPL        132  11:00:00 120.8200 120.6500 120.71650
20  2015-08-03   AAPL        133  11:05:00 120.9900 120.6500 120.83935
21  2015-08-03   AAPL        134  11:10:00 121.1800 120.9100 121.02811
22  2015-08-03   AAPL        135  11:15:00 121.1700 120.9700 121.05849
23  2015-08-03   AAPL        136  11:20:01 121.1500 121.0480 121.09072
24  2015-08-03   AAPL        137  11:25:00 121.1500 120.9500 121.04811
25  2015-08-03   AAPL        138  11:30:00 121.1200 120.9200 121.02047
26  2015-08-03   AAPL        139  11:35:00 120.9900 120.6700 120.82496
27  2015-08-03   AAPL        140  11:40:00 120.8400 120.5600 120.68603
28  2015-08-03   AAPL        141  11:45:00 120.8600 120.6600 120.75718
29  2015-08-03   AAPL        142  11:50:00 120.6800 120.3300 120.46856
30  2015-08-03   AAPL        143  11:55:00 120.5155 120.4100 120.45356
31  2015-08-03   AAPL        144  12:00:00 120.5500 120.2200 120.32099
32  2015-08-03   AAPL        145  12:05:00 120.3000 120.1100 120.20832
33  2015-08-03   AAPL        146  12:10:00 120.1734 119.9000 120.02722
34  2015-08-03   AAPL        147  12:15:00 120.0600 119.8700 119.96583
35  2015-08-03   AAPL        148  12:20:00 119.9900 119.8100 119.89728
36  2015-08-03   AAPL        149  12:25:01 120.2200 119.8900 120.03213
37  2015-08-03   AAPL        150  12:30:00 120.1800 119.9200 120.03840
38  2015-08-03   AAPL        151  12:35:00 119.9756 119.7700 119.87204
39  2015-08-03   AAPL        152  12:40:00 119.9000 119.7500 119.82387
40  2015-08-03   AAPL        153  12:45:00 119.8353 118.7000 119.16163
41  2015-08-03   AAPL        154  12:50:00 118.9890 118.2600 118.66432
42  2015-08-03   AAPL        155  12:55:00 118.5000 117.5200 117.95536
43  2015-08-03   AAPL        156  13:00:00 118.4367 117.9500 118.15729
44  2015-08-03   AAPL        157  13:05:00 118.4500 117.7101 118.08337
45  2015-08-03   AAPL        158  13:10:00 118.1200 117.6700 117.87640
46  2015-08-03   AAPL        159  13:15:00 118.0100 117.6601 117.82184
47  2015-08-03   AAPL        160  13:20:00 118.1700 117.6100 117.98232
48  2015-08-03   AAPL        161  13:25:00 118.4400 118.0200 118.27480
49  2015-08-03   AAPL        162  13:30:00 118.4500 118.2000 118.33434
50  2015-08-03   AAPL        163  13:35:00 118.5500 118.3000 118.45115
51  2015-08-03   AAPL        164  13:40:00 118.6400 118.3200 118.50168
52  2015-08-03   AAPL        165  13:45:00 118.6800 118.4400 118.54192
53  2015-08-03   AAPL        166  13:50:00 118.8300 118.6000 118.74204
54  2015-08-03   AAPL        167  13:55:00 118.8400 118.6500 118.73936
55  2015-08-03   AAPL        168  14:00:00 118.7300 118.5200 118.60532
56  2015-08-03   AAPL        169  14:05:00 118.6500 118.4300 118.50543
57  2015-08-03   AAPL        170  14:10:00 118.5800 118.4300 118.49353
58  2015-08-03   AAPL        171  14:15:01 118.7100 118.4600 118.58022
59  2015-08-03   AAPL        172  14:20:00 118.5600 118.4000 118.48883
60  2015-08-03   AAPL        173  14:25:00 118.6500 118.3300 118.49131
61  2015-08-03   AAPL        174  14:30:00 118.6675 118.4900 118.56914
62  2015-08-03   AAPL        175  14:35:00 118.6500 118.4700 118.55887
63  2015-08-03   AAPL        176  14:40:00 118.8100 118.5600 118.69735
64  2015-08-03   AAPL        177  14:45:00 118.8000 118.6200 118.72279
65  2015-08-03   AAPL        178  14:50:00 118.7800 118.6200 118.71055
66  2015-08-03   AAPL        179  14:55:00 118.7500 118.5700 118.65656
67  2015-08-03   AAPL        180  15:00:00 118.8700 118.6500 118.72095
68  2015-08-03   AAPL        181  15:05:00 118.8700 118.6900 118.78130
69  2015-08-03   AAPL        182  15:10:00 118.7200 118.4600 118.61231
70  2015-08-03   AAPL        183  15:15:01 118.6000 118.3000 118.42988
71  2015-08-03   AAPL        184  15:20:03 118.4000 118.0700 118.23301
72  2015-08-03   AAPL        185  15:25:00 118.1000 117.8600 117.99564
73  2015-08-03   AAPL        186  15:30:00 118.1900 117.9600 118.08736
74  2015-08-03   AAPL        187  15:35:00 118.1700 117.8800 117.99449
75  2015-08-03   AAPL        188  15:40:00 118.2700 118.1000 118.16962
76  2015-08-03   AAPL        189  15:45:00 118.2000 117.9600 118.08088
77  2015-08-03   AAPL        190  15:50:00 118.1700 118.0000 118.07813
78  2015-08-03   AAPL        191  15:55:00 118.4500 118.0300 118.23987
79  2015-08-03   AMZN        114  09:30:00 538.5500 536.3200 537.61255
80  2015-08-03   AMZN        115  09:35:04 537.4600 535.7700 536.42177
81  2015-08-03   AMZN        116  09:40:01 538.3500 536.4000 537.68861
82  2015-08-03   AMZN        117  09:45:00 539.3500 536.2800 537.56732
83  2015-08-03   AMZN        118  09:50:00 539.9500 538.7100 539.45455
84  2015-08-03   AMZN        119  09:55:03 540.4400 538.8100 539.66087
85  2015-08-03   AMZN        120  10:00:00 540.2600 537.7100 539.01779
86  2015-08-03   AMZN        121  10:05:00 538.4050 536.5500 537.48823
87  2015-08-03   AMZN        122  10:10:03 537.6800 536.8000 537.21956
88  2015-08-03   AMZN        123  10:15:00 536.8000 534.1500 535.24578
89  2015-08-03   AMZN        124  10:20:01 535.0500 533.7700 534.51831
90  2015-08-03   AMZN        125  10:25:04 535.3900 534.1200 534.74367
91  2015-08-03   AMZN        126  10:30:13 534.9700 534.0110 534.64140
92  2015-08-03   AMZN        127  10:35:04 535.3500 534.5001 534.92485
93  2015-08-03   AMZN        128  10:40:04 535.1600 534.1000 534.57402
94  2015-08-03   AMZN        129  10:45:02 536.0000 534.5100 535.26481
95  2015-08-03   AMZN        130  10:50:00 536.3500 535.4500 535.93280
96  2015-08-03   AMZN        131  10:55:02 536.6500 535.2600 535.96416
97  2015-08-03   AMZN        132  11:00:01 536.6000 535.6800 536.24564
98  2015-08-03   AMZN        133  11:05:10 537.8900 536.4800 537.23168
99  2015-08-03   AMZN        134  11:10:00 538.5200 537.3500 537.94708
100 2015-08-03   AMZN        135  11:15:02 537.9400 537.3500 537.58755
101 2015-08-03   AMZN        136  11:20:05 537.9050 536.8600 537.31598

我猜想有比我以前使用的Min(time)工作更优雅的解决方案。但是主要的问题是如何每隔五分钟查找一次LastPrice

更新-这是我做的一个单独的查询,它生成每五分钟一组的最后价格,但没有Min({Price),Max(Price)和Avg(Price )。我不知道如何在一个查询中组合所有这些内容。

dbGetQuery(nqdb, statement = "select t.RefDate, t.Symbol, cast(t.Timestamp/300e3 as int) as timeBucket, 
    time(t.timestamp/1000, 'unixepoch') as startTime, 
    t.price as LastPrice from trds t
    where (t.Timestamp between 34200000 and 57600000) and
    SaleCondition not glob '*[CGIHMNPQRTUVWZ47]*' group by timeBucket, symbol order by symbol ASC")



    RefDate Symbol timeBucket startTime LastPrice
1   2015-08-03   AAPL        114  09:34:00  121.2300
2   2015-08-03   AAPL        115  09:39:00  122.4400
3   2015-08-03   AAPL        116  09:44:00  122.1800
4   2015-08-03   AAPL        117  09:49:00  122.0500
5   2015-08-03   AAPL        118  09:54:00  122.0700
6   2015-08-03   AAPL        119  09:59:00  121.7700
7   2015-08-03   AAPL        120  10:04:00  121.5400
8   2015-08-03   AAPL        121  10:09:00  121.2600
9   2015-08-03   AAPL        122  10:14:00  121.5140
10  2015-08-03   AAPL        123  10:19:00  121.2700
11  2015-08-03   AAPL        124  10:24:00  121.1600
12  2015-08-03   AAPL        125  10:29:00  121.0200
13  2015-08-03   AAPL        126  10:34:00  120.9200
14  2015-08-03   AAPL        127  10:39:00  120.9900
15  2015-08-03   AAPL        128  10:44:00  120.8600
16  2015-08-03   AAPL        129  10:49:00  120.7800
17  2015-08-03   AAPL        130  10:54:00  120.6200
18  2015-08-03   AAPL        131  10:59:00  120.6500
19  2015-08-03   AAPL        132  11:04:00  120.6900
20  2015-08-03   AAPL        133  11:09:00  120.9600
21  2015-08-03   AAPL        134  11:14:00  121.0237
22  2015-08-03   AAPL        135  11:19:00  121.0899
23  2015-08-03   AAPL        136  11:24:00  121.1010
24  2015-08-03   AAPL        137  11:29:00  120.9954
25  2015-08-03   AAPL        138  11:34:00  120.9895
26  2015-08-03   AAPL        139  11:39:00  120.6750
27  2015-08-03   AAPL        140  11:44:00  120.8200
28  2015-08-03   AAPL        141  11:49:00  120.6750
29  2015-08-03   AAPL        142  11:54:00  120.4500
30  2015-08-03   AAPL        143  11:59:00  120.4900
31  2015-08-03   AAPL        144  12:04:00  120.2449
32  2015-08-03   AAPL        145  12:09:00  120.1600
33  2015-08-03   AAPL        146  12:14:00  120.0200
34  2015-08-03   AAPL        147  12:19:00  119.8700
35  2015-08-03   AAPL        148  12:24:00  119.9400
36  2015-08-03   AAPL        149  12:29:00  120.1770
37  2015-08-03   AAPL        150  12:34:00  119.9505
38  2015-08-03   AAPL        151  12:39:00  119.8400
39  2015-08-03   AAPL        152  12:44:00  119.8400
40  2015-08-03   AAPL        153  12:49:00  118.9400
41  2015-08-03   AAPL        154  12:54:00  118.4900
42  2015-08-03   AAPL        155  12:59:00  118.0000
43  2015-08-03   AAPL        156  13:04:00  118.4300
44  2015-08-03   AAPL        157  13:09:00  117.7286
45  2015-08-03   AAPL        158  13:14:00  117.9173
46  2015-08-03   AAPL        159  13:19:00  117.6700
47  2015-08-03   AAPL        160  13:24:00  118.1200
48  2015-08-03   AAPL        161  13:29:00  118.3601
49  2015-08-03   AAPL        162  13:34:00  118.4400
50  2015-08-03   AAPL        163  13:39:00  118.3000

更新:在时间戳问题上,每个@Parfait都可以正常运行,但是其中一个输出关闭了一个Symbol。同样仍然无法解决找到每五分钟组的最后价格以及最低/最高/平均水平的问题:

157 2015-08-03   DAVE        114  09:30:00  17.9600  17.9300  17.94500
158 2015-08-03   DAVE        115  09:37:00  17.9994  17.9100  17.95470
159 2015-08-03   DAVE        116  09:40:00  17.8700  17.8700  17.87000
160 2015-08-03   DAVE        118  09:50:00  17.7350  17.6950  17.71500
161 2015-08-03   DAVE        120  10:04:00  17.6900  17.6700  17.68000
162 2015-08-03   DAVE        121  10:08:00  17.5600  17.5600  17.56000
163 2015-08-03   DAVE        122  10:14:00  17.5600  17.5600  17.56000
164 2015-08-03   DAVE        124  10:24:00  17.6500  17.6500  17.65000
165 2015-08-03   DAVE        126  10:30:00  17.6000  17.5200  17.56000
166 2015-08-03   DAVE        127  10:39:00  17.4800  17.4800  17.48000
167 2015-08-03   DAVE        128  10:40:00  17.6100  17.4900  17.55000
168 2015-08-03   DAVE        129  10:47:00  17.4400  17.4400  17.44000
169 2015-08-03   DAVE        130  10:54:00  17.5600  17.5600  17.56000
170 2015-08-03   DAVE        131  10:55:00  17.4300  17.3800  17.40500
171 2015-08-03   DAVE        132  11:00:00  17.3400  17.2620  17.30720
172 2015-08-03   DAVE        133  11:09:00  17.2800  17.2700  17.27500
173 2015-08-03   DAVE        135  11:17:00  17.2700  17.2300  17.25000
174 2015-08-03   DAVE        136  11:23:00  17.3200  17.3000  17.31000
175 2015-08-03   DAVE        137  11:25:00  17.4000  17.3750  17.38875
176 2015-08-03   DAVE        139  11:39:00  17.4400  17.4400  17.44000
177 2015-08-03   DAVE        141  11:49:00  17.4200  17.4200  17.42000
178 2015-08-03   DAVE        142  11:51:00  17.4200  17.4200  17.42000
179 2015-08-03   DAVE        143  11:59:00  17.3900  17.3900  17.39000
180 2015-08-03   DAVE        144  12:01:00  17.4350  17.3600  17.39750
181 2015-08-03   DAVE        146  12:13:00  17.3700  17.3600  17.36500
182 2015-08-03   DAVE        147  12:17:00  17.3600  17.3500  17.35333
183 2015-08-03   DAVE        148  12:20:00  17.4000  17.3700  17.38500
184 2015-08-03   DAVE        149  12:25:00  17.3700  17.3700  17.37000
185 2015-08-03   DAVE        150  12:31:00  17.3300  17.3300  17.33000
186 2015-08-03   DAVE        153  12:49:00  17.3800  17.3800  17.38000
187 2015-08-03   DAVE        154  12:50:00  17.3900  17.3900  17.39000
188 2015-08-03   DAVE        155  12:55:00  17.3300  17.3300  17.33000
189 2015-08-03   DAVE        158  13:12:00  17.3400  17.3100  17.32500
190 2015-08-03   DAVE        159  13:15:00  17.3600  17.3400  17.35000
191 2015-08-03   DAVE        160  13:22:00  17.3700  17.3700  17.37000
192 2015-08-03   DAVE        162  13:34:00  17.3504  17.3504  17.35040
193 2015-08-03   DAVE        163  13:35:00  17.3300  17.3100  17.32000
194 2015-08-03   DAVE        165  13:49:00  17.3200  17.3200  17.32000
195 2015-08-03   DAVE        171  14:15:00  17.3500  17.2700  17.31563
196 2015-08-03   DAVE        172  14:20:00  17.4000  17.3500  17.36667
197 2015-08-03   DAVE        175  14:37:00  17.3900  17.3900  17.39000
198 2015-08-03   DAVE        176  14:40:00  17.4000  17.3500  17.37125
199 2015-08-03   DAVE        177  14:45:00  17.4100  17.3500  17.38300
200 2015-08-03   DAVE        178  14:51:00  17.4500  17.4500  17.45000
201 2015-08-03   DAVE        181  15:05:00  17.5800  17.5300  17.55700
202 2015-08-03   DAVE        182  15:10:00  17.5600  17.5200  17.54223
203 2015-08-03   DAVE        184  15:23:00  17.6900  17.6200  17.65500
204 2015-08-03   DAVE        186  15:30:00  17.6900  17.6900  17.69000
205 2015-08-03   DAVE        189  15:46:00  17.7500  17.7200  17.74000

以下是问题Symbol“ DAVE”的时间,以秒为单位:

  RefDate Symbol timeBucket startTime MaxPrice MinPrice AvgPrice
1  2015-08-03   DAVE        114  34200708  17.9600  17.9300 17.94500
2  2015-08-03   DAVE        115  34628936  17.9994  17.9100 17.95470
3  2015-08-03   DAVE        116  34807939  17.8700  17.8700 17.87000
4  2015-08-03   DAVE        118  35415342  17.7350  17.6950 17.71500
5  2015-08-03   DAVE        120  36250083  17.6900  17.6700 17.68000
6  2015-08-03   DAVE        121  36528404  17.5600  17.5600 17.56000
7  2015-08-03   DAVE        122  36850085  17.5600  17.5600 17.56000
8  2015-08-03   DAVE        124  37450198  17.6500  17.6500 17.65000
9  2015-08-03   DAVE        126  37892189  17.6000  17.5200 17.56000
10 2015-08-03   DAVE        127  38389126  17.4800  17.4800 17.48000
11 2015-08-03   DAVE        128  38400053  17.6100  17.4900 17.55000
12 2015-08-03   DAVE        129  38861146  17.4400  17.4400 17.44000
13 2015-08-03   DAVE        130  39250319  17.5600  17.5600 17.56000
14 2015-08-03   DAVE        131  39301007  17.4300  17.3800 17.40500
15 2015-08-03   DAVE        132  39704588  17.3400  17.2620 17.30720
16 2015-08-03   DAVE        133  40150085  17.2800  17.2700 17.27500
17 2015-08-03   DAVE        135  40668185  17.2700  17.2300 17.25000
18 2015-08-03   DAVE        136  41026622  17.3200  17.3000 17.31000
19 2015-08-03   DAVE        137  41125827  17.4000  17.3750 17.38875
20 2015-08-03   DAVE        139  41950078  17.4400  17.4400 17.44000
21 2015-08-03   DAVE        141  42550193  17.4200  17.4200 17.42000
22 2015-08-03   DAVE        142  42704053  17.4200  17.4200 17.42000
23 2015-08-03   DAVE        143  43150086  17.3900  17.3900 17.39000
24 2015-08-03   DAVE        144  43439310  17.4350  17.3600 17.39750
25 2015-08-03   DAVE        146  44086864  17.3700  17.3600 17.36500
26 2015-08-03   DAVE        147  44250485  17.3600  17.3500 17.35333
27 2015-08-03   DAVE        148  44416744  17.4000  17.3700 17.38500
28 2015-08-03   DAVE        149  44748677  17.3700  17.3700 17.37000
29 2015-08-03   DAVE        150  45087379  17.3300  17.3300 17.33000
30 2015-08-03   DAVE        153  46149940  17.3800  17.3800 17.38000
31 2015-08-03   DAVE        154  46236704  17.3900  17.3900 17.39000
32 2015-08-03   DAVE        155  46513141  17.3300  17.3300 17.33000
33 2015-08-03   DAVE        158  47589401  17.3400  17.3100 17.32500
34 2015-08-03   DAVE        159  47706529  17.3600  17.3400 17.35000
35 2015-08-03   DAVE        160  48134567  17.3700  17.3700 17.37000
36 2015-08-03   DAVE        162  48856648  17.3504  17.3504 17.35040
37 2015-08-03   DAVE        163  48943090  17.3300  17.3100 17.32000
38 2015-08-03   DAVE        165  49755204  17.3200  17.3200 17.32000
39 2015-08-03   DAVE        171  51379248  17.3500  17.2700 17.31563
40 2015-08-03   DAVE        172  51622647  17.4000  17.3500 17.36667
41 2015-08-03   DAVE        175  52636324  17.3900  17.3900 17.39000
42 2015-08-03   DAVE        176  52921459  17.4000  17.3500 17.37125
43 2015-08-03   DAVE        177  53132520  17.4100  17.3500 17.38300
44 2015-08-03   DAVE        178  53516691  17.4500  17.4500 17.45000
45 2015-08-03   DAVE        181  54350810  17.5800  17.5300 17.55700
46 2015-08-03   DAVE        182  54749749  17.5600  17.5200 17.54223
47 2015-08-03   DAVE        184  55476188  17.6900  17.6200 17.65500
48 2015-08-03   DAVE        186  55804762  17.6900  17.6900 17.69000
49 2015-08-03   DAVE        189  56789594  17.7500  17.7200 17.74000
50 2015-08-03   DAVE        190  57274050  17.7800  17.6600 17.73333
51 2015-08-03   DAVE        191  57557752  17.7000  17.6300 17.66265

1 个答案:

答案 0 :(得分:1)

请考虑使用模运算符%,将其余数减去最近的分钟(即60秒):

Min(time(t.timestamp/1000 - (t.timestamp/1000) % 60, 'unixepoch')) as startTime

此外,要查找第一个或最后一个价格,请使用时间戳运行多个聚合,然后将聚合加入主查询。下面使用CTE,它应该在最新的SQLite版本中可用。将整个查询包装在R字符串中,它应该返回最后一个SELECT结果集作为数据帧。

以下使用新的 five_min_grp 并运行聚合查询的自连接,但与主体级查询在不同的时间戳上连接。

with unit as
   (select t.RefDate, t.Symbol, cast(t.Timestamp/300e3 as int) as timeBucket,
           time(t.timestamp/1000 - (t.timestamp/1000) % 300, 'unixepoch') as five_min_grp, 
           time(t.timestamp/1000 - (t.timestamp/1000) % 60, 'unixepoch') as startTime, 
           t.price as Price 
    from trds t
    where (t.Timestamp between 34200000 and 57600000) 
      and (t.SaleCondition not glob '*[CGIHMNPQRTUVWZ47]*') 
   ),

   agg as
   (select u.RefDate, u.Symbol, u.five_min_grp, 
           min(u.startTime) as first_startTime,
           max(u.startTime) as last_startTime
    from unit u
    group by u.RefDate, u.Symbol, u.five_min_grp
   ),   

select u.RefDate, u.Symbol, u.five_min_grp, 
       avg(u.price) as AvgPrice,
       min(u.price) as MinPrice,
       max(u.price) as MaxPrice,
       min(case when first.first_startTime is not null 
                then u.price
                else null
           end) as FirstPrice,
       max(case when last.last_startTime is not null 
                then u.price
                else null
           end) as LastPrice

from unit u

left join agg first
  on  first.RefDate = u.RefDate 
  and first.Symbol = u.Symbol
  and first.five_min_grp = u.five_min_grp
  and first.first_startTime = u.startTime

left join agg last
  on  last.RefDate = u.RefDate 
  and last.Symbol = u.Symbol
  and last.five_min_grp = u.five_min_grp
  and last.last_startTime = u.startTime

group by u.RefDate, u.Symbol, u.five_min_grp
order by u.symbol

DB Fiddle Demo-不使用CTE,因为其SQLite版本已过时