Example of creating a virtual table
This example shows how to create a virtual table on a table that contains time series data and the difference between querying the base table and the virtual table.
To improve clarity, these examples use values t1 through t6 to indicate DATETIME values, rather than showing complete DATETIME strings.
Query the base table
create row type stock_bar(
timestamp datetime year to fraction(5),
high real,
low real,
final real,
vol real
);
create table daily_stocks (
stock_id int,
stock_name lvarchar,
stock_data TimeSeries(stock_bar)
);
stock_id | stock_name | stock_data |
---|---|---|
900 | AA01 | (t1, 7.25, 6.75, 7, 1000000), (t2, 7.5, 6.875, 7.125, 1500000), ... |
901 | IBM® | (t1, 97, 94.25, 95, 2000000), (t2, 97, 95.5, 96, 3000000), ... |
905 | FNM | (t1, 49.25, 47.75, 48, 2500000), (t2, 48.75, 48, 48.25, 3000000), ... |
select stock_id,
Apply('$final', stock_data)::TimeSeries(one_real)
from daily_stocks;
create row type one_real(
timestamp datetime year to fraction(5),
result real);
select stock_id, Clip(stock_data, t1, t2) from daily_stocks;
Create the virtual table
execute procedure
TSCreateVirtualTab('daily_stocks_no_ts', 'daily_stocks');
Because the statement does not specify the NewTimeSeries parameter, daily_stocks_no_ts does not allow inserts of elements that do not have a corresponding time series in daily_stocks.
Also, the statement omits the TSVTMode parameter,
so TSVTMode assumes its default value of 0
.
Therefore, if you insert data into daily_stocks_no_ts, the
database server uses PutElemNoDups to add an element
to the underlying time series in daily_stocks.
stock_id | stock_name | timestamp* | high | low | final | vol |
---|---|---|---|---|---|---|
900 | AA01 | t1 | 7.25 | 6.75 | 7 | 1000000 |
900 | AA01 | t2 | 7.5 | 6.875 | 7.125 | 1500000 |
... | ... | ... | ... | ... | ||
901 | IBM® | t1 | 97 | 94.25 | 95 | 2000000 |
901 | IBM® | t2 | 97 | 95.5 | 96 | 3000000 |
... | ... | ... | ... | ... | ||
905 | FNM | t1 | 49.25 | 47.75 | 48 | 2500000 |
905 | FNM | t2 | 48.75 | 48 | 48.25 | 3000000 |
... | ... | ... | ... |
Query the virtual table
select stock_id, final from daily_stocks_no_ts;
select * from daily_stocks_no_ts
where timestamp between t1 and t5;
select * from daily_stocks_no_ts
where timestamp between t1 and t5
order by volume;
insert into daily_stock_no_ts
values('IBM', t6, 55, 53, 54, 2000000);
The element (t6, 55, 53, 54, 2000000) is added to daily_stocks.