CREATE VIEW¶
Valid in: SQL, ESQL, OpenAPI, ODBC, JDBC, .NET
The CREATE VIEW statement defines a virtual table.
This statement has the following format:
- view_name
-
Defines the name of the view. It must be a valid object name.
- select_stmt
-
Is a SELECT statement, as described in the SELECT statement description in this chapter.
CREATE VIEW Example¶
Create history tables and a current table with a view over all of them. Insert values. Select data from the current table and from the master view. Update some data and perform the select again.
INSERT INTO current (id, name, effective_start) VALUES (1,'tins','2012-01-23'),(2,'bags','2012-01-30'),(3,'boxes','2012-01-30');
(1,'tins','2010-01-23','2011-09-30'),(2,'bags','2010-01-30','2010-11-01'),(3,'boxes','2010-12-25','2011-03-20');
(1,'tins','2011-09-30','2012-01-23'),(2,'bags','2010-11-01','2012-01-30'),(3,'boxes','2011-03-20','2012-01-30');
SELECT id, MAX(name), MAX(effective_start), MAX(CASE WHEN IFNULL(effective_end,'0001-01-01') = '0001-01-01' THEN 'CURRENT' ELSE CHAR(effective_end) END) FROM current GROUP BY id ORDER BY 2;
SELECT id, MAX(name), MAX(effective_start), MAX(effective_end) FROM history_2011 GROUP BY id ORDER BY 2;
SELECT id, MAX(name), MAX(effective_start), MAX(CASE WHEN IFNULL(effective_end,'0001-01-01') = '0001-01-01' THEN 'CURRENT' ELSE CHAR(effective_end) END) FROM current GROUP BY id ORDER BY 2;
SELECT id, MAX(name), MAX(effective_start), MAX(effective_end) FROM history_2011 GROUP BY id ORDER BY 2;