I have a (Sybase) table that has the following information:
order_id int
timestamp datetime
action char(1) --i=inserted, c=corrected, r=removed
shares int
It keeps track of the shares associated with an order (identified by its order_id) in a system. Using an example, the life of an order is as follows:
timestamp action shares
10:00:00 i 1000 -- initial Insert
10:06:30 c 900 -- one Change
10:07:12 c 800
10:50:20 r 800 -- Removal
11:10:10 i 600 -- 2nd Insert
11:12:10 r 600
In the example above, the order is active from 10:00:00 and 10:50:20, and again from 11:10:10 and 11:12:10
I have 1000s of such orders in the system and I need to plot with a histogram how many shares are active in a time series divided into bins/buckets of 5 minutes. If the number of shares for a given order changes more than once within the samenter code heree bin, I need to average the shares; as in the example above in the 10:05-10:10 bin where 1000, 900 and 800 can be avg'd out as 900.
Here's a more complex example:
1, "20140828 10:00:00", "i", 1000
1, "20140828 10:06:00", "c", 900
1, "20140828 10:07:12", "c", 500
1, "20140828 10:10:10", "c", 400
1, "20140828 10:20:20", "r", 400
1, "20140828 10:30:10", "i", 300
1, "20140828 10:32:10", "r", 300
2, "20140828 09:51:00", "i", 500
2, "20140828 10:08:30", "r", 500
3, "20140828 10:10:00", "i", 1000
3, "20140828 10:11:20", "r", 1000
with its expected output:
10:00:00 1500
10:05:00 1300
10:10:00 1450
10:15:00 400
10:20:00 400
10:25:00 0
10:30:00 300
10:35:00 0
10:40:00 0
10:45:00 0
10:50:00 0
10:55:00 0
Thanks in advance for your help.