We help IT Professionals succeed at work.

Inserting transaction data into various time related tables

GNOVAK
GNOVAK asked
on
I have posted this question and received an answer, however I now have a new problem. Here;s the original question:
---------
I have a table that has trading transactions and contains a timestamp field and a ASK price each time the price changes (tick data).
The timestamp has the format:1/1/2009 10:42:00.000000 PM
The records are sequential but do not have a consistent gap. some are the same time, some are 2 seconds apart, some 10 seconds apart.
I wish to load  the beginning ask price and the ending ask price for each minute into another table (MIN_Table)

-------
The answer that worked great:
SELECT   TO_CHAR (ticktime, 'dd/mm/yyyy hh24:mi'),
           Min(ASK) Min,
           Max(ASK) Max,
           MAX (ask) KEEP (DENSE_RANK FIRST ORDER BY id) Open,
           MAX (ask) KEEP (DENSE_RANK LAST ORDER BY id)  Close
    FROM   Ticktemp
GROUP BY   TO_CHAR (ticktime, 'dd/mm/yyyy hh24:mi');
------
I place this into an INSERT INTO statement and this works for a table containing 1 minute transactions.
I can also make this work for a 1 hour table (thanks to help here) by changing the TO_CHAR to hh24:hh24

But I need to be able to generate data to fill the following individual tables:
1 min (got it)
5 min
15 min
30 min
1 hour(got it)
4 hours
1 day
1 week
1 month.

Does anyone have a solution for the other time frame tables that I can use in an insert statement?
Comment
Watch Question

Commented:
Hi!
Here is a possible solution:

For 5,15,30 min:
SELECT trunc(SYSDATE) + round(TO_CHAR (SYSDATE, 'SSSSS')/60/15 )/24/60*15  FROM DUAL;

This will round current date to 15 minutes. Just change number 15 with 5 or 30 for other intervals

So query would look like:

SELECT trunc(ticktime) + round(TO_CHAR (ticktime, 'SSSSS')/60/15 )/24/60*15
           Min(ASK) Min,
           Max(ASK) Max,
           MAX (ask) KEEP (DENSE_RANK FIRST ORDER BY id) Open,
           MAX (ask) KEEP (DENSE_RANK LAST ORDER BY id)  Close
    FROM   Ticktemp
GROUP BY   trunc(ticktime) + round(TO_CHAR (ticktime, 'SSSSS')/60/15 )/24/60*15

(If you need it as string just put TO_CHAR (...,'dd/mm/yyyy hh24:mi') in select statement...
Commented:
For 1 day use: trunc(ticktime)
For 1 week use: trunc(ticktime,'W')
For 1 month use: trunc(ticktime,'MM')

Correction in above query: instead of round use trunc:
trunc(ticktime) + trunc(TO_CHAR (ticktime, 'SSSSS')/60/15 )/24/60*15

For 4 hours use 240 instead of 15.

Regards...

Author

Commented:
Excellent - thanks so much! worked like a charm

Author

Commented:
If you're still around, I have a question.
Should the week be tunc(ticktime,'WW')?  When I run it today with 'W', it gives me yesterday's date (tuesday), if I run it wilt 'WW' it gives me last saturday's date.


Commented:
Oops, yes you are right...

According to
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions230.htm#i1002084

it seems that you might try trunc(ticktime,"D") , I don't have oracle at hand right now, but it says that it should trunc to starting day of week (sunday or monday, depending on your NSL settings)

Author

Commented:
ah, but how would I do a weekly where the where the week starts on a certain day, i.e. Sunday, no mater where the year started?

Commented:
If you use "D" it does not depend on day when week started but it depends on how is first day of week defined (i.e. sunday or monday) as configured in your oracle system.

Check

http://download.oracle.com/docs/cd/E11882_01/server.112/e25513/initparams153.htm#REFRN10128
http://download.oracle.com/docs/cd/E11882_01/server.112/e10729/ch3globenv.htm#i1006753

about parameter NLS_TERRITORY

Explore More ContentExplore courses, solutions, and other research materials related to this topic.