Reputation: 631
Suppose I have a stream that contains stock prices. Each record consists of
ticker
price
e.g.
I need to fetch last 60 price records for AAPL. I don't want to use time windows - just last 60 records.
Upvotes: 0
Views: 1474
Reputation: 1893
You can build a table that tracks the last 60 prices, per-ticker, using something like:
CREATE TABLE RECENT_PRICES AS
SELECT
tickerId,
LATEST_BY_OFFSET(price, 60) AS PRICES
FROM TICKS
GROUP BY tickerId;
The PRICES
column will be an ARRAY<DECIMAL(?, ?)>
holding the latest 60 prices.
Note: this is latest by offset, not by timestamp. So out-of-order data may cause problems.
Upvotes: 1