This topic describes how to create a normal view in ApsaraDB for ClickHouse.

Create a view

Syntax
CREATE VIEW [IF NOT EXISTS] [db.]view_name [ON CLUSTER cluster] AS SELECT ...
Parameters
ParameterDescription
dbThe name of the database. The default value is the name of the current database. In this example, default is used as the database name.
view_nameThe name of the view.
[ON CLUSTER cluster]Specifies that a view is created on each node. Set the value to ON CLUSTER default.
SELECT ...The SELECT clause. When you insert data into the source table that is specified in the SELECT clause in the view, the inserted data is transformed by the SELECT query and the final result is inserted into the view.
Note A SELECT query can contain DISTINCT, GROUP BY, ORDER BY, and LIMIT. The corresponding transformations are performed independently on each block of the data that is inserted.
Example
  1. Create the source table that is specified by the SELECT clause.
    create table test ON CLUSTER default (
      id Int32,
      name String
    ) ENGINE = MergeTree()
        ORDER BY (id);
  2. Create a view based on the source table.
    CREATE VIEW test_view ON CLUSTER default AS SELECT * FROM test;
  3. Write data to the source table.
    insert into test values(1,'a'),(2,'b'),(3,'c');
  4. Query the view.
    SELECT * FROM test_view;
    The following query result is returned:
    id│name
    ─│──
     1│ a
     2│ b 
     3│ c

References

For information about how to create a view, see Create View.