

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# CTAS 查詢的範例
<a name="ctas-examples"></a>

使用下列範例來建立 CTAS 查詢。如需 CTAS 語法的相關資訊，請參閱[CREATE TABLE AS](create-table-as.md)。

在本節中：
+  [Example: Duplicating a table by selecting all columns](#ctas-example-dupe-table) 
+  [Example: Selecting specific columns from one or more tables](#ctas-example-specify-columns) 
+  [Example: Creating an empty copy of an existing table](#ctas-example-empty-table) 
+  [Example: Specifying data storage and compression formats](#ctas-example-compression) 
+  [Example: Writing query results to a different format](#ctas-example-format) 
+  [Example: Creating unpartitioned tables](#ctas-example-unpartitioned) 
+  [Example: Creating partitioned tables](#ctas-example-partitioned) 
+  [Example: Creating bucketed and partitioned tables](#ctas-example-bucketed) 
+  [Example: Creating an Iceberg table with Parquet data](#ctas-example-iceberg-parquet) 
+  [Example: Creating an Iceberg table with Avro data](#ctas-example-iceberg-avro) 
+  [Example: Creating an S3 table using CTAS](#ctas-example-s3-table) 

**Example 範例：選取所有資料欄以複製資料表**  
以下範例會透過複製資料表的所有資料欄來建立資料表：  

```
CREATE TABLE new_table AS 
SELECT * 
FROM old_table;
```
在相同範例的以下變化中，您的 `SELECT` 陳述式也包含 `WHERE` 子句。在這種情況下，查詢只會從資料表中選取滿足 `WHERE` 子句的那些資料列：  

```
CREATE TABLE new_table AS 
SELECT * 
FROM old_table 
WHERE condition;
```

**Example 範例：從一或多個資料表選取特定資料欄**  
以下範例會建立對來自另一個資料表的一組資料欄執行的新查詢：  

```
CREATE TABLE new_table AS 
SELECT column_1, column_2, ... column_n 
FROM old_table;
```
相同範例的這個變化會來自多個資料表的特定資料欄建立新的資料表：  

```
CREATE TABLE new_table AS
SELECT column_1, column_2, ... column_n 
FROM old_table_1, old_table_2, ... old_table_n;
```

**Example 範例：建立現有資料表的空白副本**  
以下範例使用 `WITH NO DATA` 來建立空白且與原始資料表具有相同結構描述的新資料表：  

```
CREATE TABLE new_table 
AS SELECT * 
FROM old_table
WITH NO DATA;
```

**Example 範例：指定資料儲存和壓縮格式**  
利用 CTAS，您可以使用一種儲存格式的來源資料表，建立不同儲存格式的另一個資料表。  
使用 `format` 屬性來指定 `ORC`、`PARQUET`、`AVRO`、`JSON` 或 `TEXTFILE` 作為新資料表的儲存格式。  
對於 `PARQUET`、`ORC`、`TEXTFILE`，和 `JSON` 儲存格式，請使用 `write_compression` 屬性指定新資料表資料的壓縮格式。如需每個檔案格式支援之壓縮格式的詳細資訊，請參閱[在 Athena 中使用壓縮](compression-formats.md)。  
下列範例指定資料表 `new_table` 中的資料以 Parquet 格式儲存並使用 Snappy 壓縮。Parquet 的預設壓縮為 `GZIP`。  

```
CREATE TABLE new_table
WITH (
      format = 'Parquet',
      write_compression = 'SNAPPY')
AS SELECT *
FROM old_table;
```
下列範例指定使用 Snappy 壓縮以 ORC 格式儲存資料表 `new_table` 中的資料。ORC 的預設壓縮為 ZLIB。  

```
CREATE TABLE new_table
WITH (format = 'ORC',
      write_compression = 'SNAPPY')
AS SELECT *
FROM old_table ;
```
下列範例指定使用 Snappy 壓縮以文字檔案格式儲存資料表 `new_table` 中的資料。Textfile 和 JSON 格式的預設壓縮皆為 GZIP。  

```
CREATE TABLE new_table
WITH (format = 'TEXTFILE',
      write_compression = 'SNAPPY')
AS SELECT *
FROM old_table ;
```

**Example 範例：將查詢結果寫入不同的格式**  
以下 CTAS 查詢會從 `old_table` 選取所有記錄 (可用 CSV 或其他格式儲存)，並使用以 ORC 格式儲存至 Simple Storage Service (Amazon S3) 的基礎資料建立新的資料表：  

```
CREATE TABLE my_orc_ctas_table
WITH (
      external_location = 's3://amzn-s3-demo-bucket/my_orc_stas_table/',
      format = 'ORC')
AS SELECT * 
FROM old_table;
```

**Example 範例：建立未分割的資料表**  
以下範例會建立未分割的資料表。資料表資料會以不同格式存放。其中的一些範例會指定外部位置。  
以下範例會建立可將結果存放為文字檔案的 CTAS 查詢：  

```
CREATE TABLE ctas_csv_unpartitioned 
WITH (
     format = 'TEXTFILE', 
     external_location = 's3://amzn-s3-demo-bucket/ctas_csv_unpartitioned/') 
AS SELECT key1, name1, address1, comment1
FROM table1;
```
在下列範例中，結果會存放在 Parquet 中，並且會使用預設結果位置：  

```
CREATE TABLE ctas_parquet_unpartitioned 
WITH (format = 'PARQUET') 
AS SELECT key1, name1, comment1
FROM table1;
```
在以下查詢中，資料表會存放在 JSON 中，並且從原始資料表的結果選取特定資料欄：  

```
CREATE TABLE ctas_json_unpartitioned 
WITH (
     format = 'JSON',  
     external_location = 's3://amzn-s3-demo-bucket/ctas_json_unpartitioned/') 
AS SELECT key1, name1, address1, comment1
FROM table1;
```
在下列範例中，格式為 ORC：  

```
CREATE TABLE ctas_orc_unpartitioned 
WITH (
     format = 'ORC') 
AS SELECT key1, name1, comment1 
FROM table1;
```
在下列範例中，格式為 Avro：  

```
CREATE TABLE ctas_avro_unpartitioned 
WITH (
     format = 'AVRO', 
     external_location = 's3://amzn-s3-demo-bucket/ctas_avro_unpartitioned/') 
AS SELECT key1, name1, comment1
FROM table1;
```

**Example 範例：建立分割的資料表**  
以下範例顯示採用不同儲存格式的分割資料表的 `CREATE TABLE AS SELECT` 查詢，使用 `partitioned_by`，以及 `WITH` 子句中的其他屬性。如需語法，請參閱[CTAS 資料表屬性](create-table-as.md#ctas-table-properties)。如需選擇用於分割的資料欄的詳細資訊，請參閱[使用分割和歸納](ctas-partitioning-and-bucketing.md)。  
在 `SELECT` 陳述式中資料欄清單結尾列出分割區資料欄。您可以依多於一個資料欄分割，且最多可以有 100 個唯一分割區和儲存貯體組合。例如，如果未指定儲存貯體，則可有 100 個分割區。

```
CREATE TABLE ctas_csv_partitioned 
WITH (
     format = 'TEXTFILE',  
     external_location = 's3://amzn-s3-demo-bucket/ctas_csv_partitioned/', 
     partitioned_by = ARRAY['key1']) 
AS SELECT name1, address1, comment1, key1
FROM tables1;
```

```
CREATE TABLE ctas_json_partitioned 
WITH (
     format = 'JSON', 
     external_location = 's3://amzn-s3-demo-bucket/ctas_json_partitioned/', 
     partitioned_by = ARRAY['key1']) 
AS select name1, address1, comment1, key1 
FROM table1;
```

**Example 範例：建立歸納和分割的資料表**  
以下範例顯示一個 `CREATE TABLE AS SELECT` 查詢，同時使用歸納和分割來在 Simple Storage Service (Amazon S3) 中存放查詢結果。資料表結果會依不同的資料欄分割和歸納。Athena 支援最多 100 個唯一儲存貯體和分割區組合。例如，如果您建立包含 5 個儲存貯體的資料表，則支援 20 個分割區 (每個都有 5 個儲存貯體)。如需語法，請參閱[CTAS 資料表屬性](create-table-as.md#ctas-table-properties)。  
如需選擇用於歸納的資料欄的詳細資訊，請參閱[使用分割和歸納](ctas-partitioning-and-bucketing.md)。  

```
CREATE TABLE ctas_avro_bucketed 
WITH (
      format = 'AVRO', 
      external_location = 's3://amzn-s3-demo-bucket/ctas_avro_bucketed/', 
      partitioned_by = ARRAY['nationkey'], 
      bucketed_by = ARRAY['mktsegment'], 
      bucket_count = 3) 
AS SELECT key1, name1, address1, phone1, acctbal, mktsegment, comment1, nationkey 
FROM table1;
```

**Example 範例：建立一個包含 Parquet 資料的 Iceberg 資料表。**  
下列範例會建立包含 Parquet 資料檔案的 Iceberg 資料表。使用 `table1` 中的 `dt` 資料欄按月份分割檔案。此範例會更新資料表上的保留屬性，以便在資料表中的每個分支上保留 10 個快照。另外，還會保留過去 7 天內的快照。如需有關 Athena 中的 Iceberg 資料表屬性的詳細資訊，請參閱 [指定資料表屬性](querying-iceberg-creating-tables.md#querying-iceberg-table-properties)。  

```
CREATE TABLE ctas_iceberg_parquet
WITH (table_type = 'ICEBERG',
      format = 'PARQUET', 
      location = 's3://amzn-s3-demo-bucket/ctas_iceberg_parquet/', 
      is_external = false,
      partitioning = ARRAY['month(dt)'],
      vacuum_min_snapshots_to_keep = 10,
      vacuum_max_snapshot_age_seconds = 604800
   ) 
AS SELECT key1, name1, dt FROM table1;
```

**Example 範例：建立一個包含 Avro 資料的 Iceberg 資料表。**  
下列範例會建立包含 Avro 資料檔案的 Iceberg 資料表，其中資料表會依 `key1` 進行分割。  

```
CREATE TABLE ctas_iceberg_avro
WITH ( format = 'AVRO', 
       location = 's3://amzn-s3-demo-bucket/ctas_iceberg_avro/', 
       is_external = false,
       table_type = 'ICEBERG',
       partitioning = ARRAY['key1']) 
AS SELECT key1, name1, date FROM table1;
```

**Example 範例：使用 CTAS 建立 S3 資料表**  
以下範例會使用 CTAS 建立 S3 資料表。請注意，位置屬性會被省略，且 `table_type` 預設為 `ICEBERG`：  

```
CREATE TABLE "s3tablescatalog/amzn-s3-demo-bucket"."namespace"."s3-table-name"
WITH (
    format = 'PARQUET'
)
AS SELECT *
FROM source_table;
```
您可以指定所有其他 Iceberg 資料表屬性，例如使用與一般 Iceberg 資料表相同的語法進行分割和歸納。