Similarly to the
XCOL table type,
OCCUR is an extension to the
PROXY type when
referring to a table or view having several columns containing the same kind of
data. It enables having a different view of the table where the data from
these columns are put in a single column, eventually causing several rows to be
generated from one row of the object table. For example, supposing we have a
We can create an occur table by:
create table xpet ( name varchar(12) not null, race char(6) not null, number int not null) engine=connect table_type=occur tabname=pets option_list='OccurCol=number,RankCol=race' Colist='dog,cat,rabbit,bird,fish';
When displaying it by
select * from xpet;
We will get the result:
First of all, the values of the column listed in the Colist option have been put in a unique column whose name is given by the OccurCol option. When several columns have non null (or pseudo-null) values, several rows are generated, with the other normal columns values repeated.
In addition, an optional special column was added whose name is given by the RankCol option. This column contains the name of the source column from which the value of the OccurCol column comes from. It permits here to know the race of the pets whose number is given in number.
This table type permit to make queries that would be more complicated to make on the original tables. For instance to know who as more than 1 pet of a kind, you can simply ask:
select * from xpet where number > 1;
You will get the result:
Note 1: Like for XCOL tables, no row multiplication for queries not implying the Occur column.
Note 2: Because the OccurCol was declared "not null" no rows were generated for null or pseudo-null values of the column list. If the OccurCol is declared as nullable, rows are also generated for columns containing null or pseudo-null values.
Occur tables can be also defined from views or source definition. Also, CONNECT is able to generate the column definitions if not specified. For example:
create table ocsrc engine=connect table_type=occur colist='january,february,march,april,may,june,july,august,september, october,november,december' option_list='rankcol=month,occurcol=day' srcdef='select ''Foo'' name, 8 january, 7 february, 2 march, 1 april, 8 may, 14 june, 25 july, 10 august, 13 september, 22 october, 28 november, 14 december';
This table is displayed as: