I have a table of orders with a column denoting whether it's a buy or a sell, with the rows typically ordered by timestamp. What I'd like to do is operate on groups of consecutive buys, plus their sell. e.g. B B S B S B B S -> (B B S) (B S) (B B S)
Example:
order_action | timestamp
-------------+---------------------
buy | 2013-10-03 13:03:02
buy | 2013-10-08 13:03:02
sell | 2013-10-10 15:58:02
buy | 2013-11-01 09:30:02
buy | 2013-11-01 14:03:02
sell | 2013-11-07 10:34:02
buy | 2013-12-03 15:46:02
sell | 2013-12-09 16:00:03
buy | 2013-12-11 13:02:02
sell | 2013-12-18 15:59:03
I'll be running an aggregation function in the end (the groups are so that I can exclude an entire group based on its sell order), so GROUP BY
or partitioned windows seemed like the right way to go, but I can't figure out how to get this specific grouping.