CREATE TABLE AS

摘要

  1. CREATE TABLE [ IF NOT EXISTS ] table_name [ ( column_alias, ... ) ]
  2. [ COMMENT table_comment ]
  3. [ WITH ( property_name = expression [, ...] ) ]
  4. AS query
  5. [ WITH [ NO ] DATA ]

说明

创建一个包含 SELECT 查询结果的表。使用 CREATE TABLE 可以创建空表。

如果使用可选的 IF NOT EXISTS 子句,则在表已存在时禁止显示错误。

可以使用可选的 WITH 子句来设置创建的表的属性。要列出所有可用的表属性,请运行以下查询:

  1. SELECT * FROM system.metadata.table_properties

示例

使用查询结果和给定的列名创建表 orders_column_aliased

  1. CREATE TABLE orders_column_aliased (order_date, total_price)
  2. AS
  3. SELECT orderdate, totalprice
  4. FROM orders

创建对 orders 进行汇总的表 orders_by_date

  1. CREATE TABLE orders_by_date
  2. COMMENT 'Summary of orders by date'
  3. WITH (format = 'ORC')
  4. AS
  5. SELECT orderdate, sum(totalprice) AS price
  6. FROM orders
  7. GROUP BY orderdate

如果表 orders_by_date 尚不存在,则创建该表:

  1. CREATE TABLE IF NOT EXISTS orders_by_date AS
  2. SELECT orderdate, sum(totalprice) AS price
  3. FROM orders
  4. GROUP BY orderdate

创建模式与 nation 相同的表 empty_nation,但表中不含数据。

  1. CREATE TABLE empty_nation AS
  2. SELECT *
  3. FROM nation
  4. WITH NO DATA

另请参见

CREATE TABLESELECT