I have high frequency tick data which need to sort from microsecond to 6 seconds interval. The daily tick data start from 09:15:00.000 EST, and end at 15:15:00.000 EST.
This is my temp2 table:
date1 | close1 | volume1
-----------------------------+--------+---------
2010-04-16 09:15:28.010 EST | 10001 | 4
2010-04-16 09:16:00.020 EST | 10002 | 5
2010-04-16 09:16:35.030 EST | 10003 | 6
2010-04-16 09:16:35.040 EST | 10001 | 3
2010-04-16 15:14:59.050 EST | 10007 | 3
2010-04-19 09:15:05.050 EST | 10002 | 1
... | ... | ...
(date1 is a VARCHAR2(28); close1 and volume1 are both NUMBERs).
How do I get the following result?
date2 | close2 | volume2
---------------------+--------+---------
2010-04-16 09:15:30 | 10001 | 4
2010-04-16 09:16:06 | 10002 | 5
2010-04-16 09:16:36 | 10001 | 9
2010-04-16 15:15:00 | 10007 | 3
2010-04-19 09:15:06 | 10002 | 1
... | ... | ...
The close2 column use last_value of interval 00, 06, 12, 18, 24, 30, 36, 42, 48, 54. and if microseconds in range 00-05.999, just set close2 column value as last_vlue of the interval, set volume2 column value as sum of volume1 of the interval.
Rather than packing all the logic into a single query — which is possible, but incredibly ugly and painful — I think it makes sense to create a stored-procedure that translates your
date1to yourdate2:Then the rest, while not exactly straightforward, is nonetheless palatable:
(You can actually do it with just one subquery, by substituting the definition of
date2everywhere — and maybe even without any subquery at all, if theWHERE rn = 1can be converted into aHAVINGclause, which I didn’t try — but I think this is the clearest way.)