

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

# AWS IoT SiteWise 查詢語言
<a name="sql"></a>

透過 AWS IoT SiteWise 資料擷取 [ExecuteQuery](https://docs.aws.amazon.com/iot-sitewise/latest/APIReference/API_ExecuteQuery.html) API 操作，您可以從下列項目擷取宣告性結構定義的相關資訊，以及與其相關聯的時間序列資料：
+  模型
+ 資產
+ 測量
+ 指標
+ 轉換
+ 彙總

這可以在單一 API 請求中使用 SQL，例如查詢陳述式。

**注意**  
此功能適用於所有 AWS IoT SiteWise 可用區域，但 AWS GovCloud （美國西部）、加拿大 （中部）、中國 （北京） 和美國東部 （俄亥俄） 除外。

**Topics**
+ [的查詢語言參考 AWS IoT SiteWise](query-reference.md)

# 的查詢語言參考 AWS IoT SiteWise
<a name="query-reference"></a>

 AWS IoT SiteWise 支援豐富的查詢語言以使用您的資料。下列主題說明可用的資料類型、運算子、函數和建構。

請參閱 [查詢範例](sql-examples.md) 以使用查詢語言撰寫 AWS IoT SiteWise 查詢。

**Topics**
+ [查詢參考檢視](query-reference-views.md)
+ [支援的資料類型](supported-data-types.md)
+ [支援的子句](supported-clauses.md)
+ [邏輯運算子](sql-supported-logical.md)
+ [比較運算子](sql-supported-comparision.md)
+ [SQL 函數](sql-functions.md)
+ [查詢範例](sql-examples.md)

# 查詢參考檢視
<a name="query-reference-views"></a>

本節提供的資訊可協助您了解 中的檢視 AWS IoT SiteWise，例如程序中繼資料和遙測資料。

下表提供檢視的檢視名稱和說明：


**資料模型**  

|  **檢視名稱**  |  **檢視說明**  | 
| --- | --- | 
|  資產  |  包含資產和模型衍生的相關資訊。  | 
|  asset\$1property  |  包含資產屬性結構的相關資訊。  | 
|  raw\$1time\$1series  |  包含時間序列的歷史資料。  | 
|  latest\$1value\$1time\$1series  |  包含時間序列的最新值。  | 
|  precomputed\$1aggregates  |  包含自動計算的彙總資產屬性值。它們是一組在多個時間間隔內計算的基本指標。  | 

下列檢視會列出每個檢視的資料欄名稱和資料類型。


**檢視：資產**  

|  **資料欄名稱**  |  **datatype**  | 
| --- | --- | 
|  asset\$1id  |  string  | 
|  asset\$1name  |  string  | 
|  asset\$1description  |  string  | 
|  asset\$1model\$1id  |  string  | 
|  parent\$1asset\$1id  |  string  | 
| asset\$1external\$1id | string | 
| asset\$1model\$1external\$1id | string | 
| hierarchy\$1id | string | 


**檢視：asset\$1property**  

|  **資料欄名稱**  |  **datatype**  | 
| --- | --- | 
|  asset\$1id  |  string  | 
|  property\$1id  |  string  | 
|  property\$1name  |  string  | 
|  property\$1alias  |  string  | 
|  property\$1external\$1id  |  string  | 
|  asset\$1composite\$1model\$1id  |  string  | 
|  property\$1type  |  string  | 
|  property\$1data\$1type  |  string  | 
|  int\$1attribute\$1value  |  integer  | 
|  double\$1attribute\$1value  |  double  | 
|  boolean\$1attribute\$1value  |  boolean  | 
|  string\$1attribute\$1value  |  string  | 


**檢視：raw\$1time\$1series**  

|  **資料欄名稱**  |  **datatype**  | 
| --- | --- | 
|  asset\$1id  |  string  | 
|  property\$1id  |  string  | 
|  property\$1alias  |  string  | 
|  event\$1timestamp  |  timestamp  | 
|  quality  |  string  | 
|  boolean\$1value  |  boolean  | 
|  int\$1value  |  integer  | 
|  double\$1value  |  double  | 
|  string\$1value  |  string  | 


**檢視：latest\$1value\$1time\$1series**  

|  **資料欄名稱**  |  **datatype**  | 
| --- | --- | 
|  asset\$1id  |  string  | 
|  property\$1id  |  string  | 
|  property\$1alias  |  string  | 
|  event\$1timestamp  |  timestamp  | 
|  quality  |  string  | 
|  boolean\$1value  |  boolean  | 
|  int\$1value  |  integer  | 
|  double\$1value  |  double  | 
|  string\$1value  |  string  | 


**檢視：precomputed\$1aggregates**  

|  **資料欄名稱**  |  **datatype**  | 
| --- | --- | 
|  asset\$1id  |  string  | 
|  property\$1id  |  string  | 
|  property\$1alias  |  string  | 
|  event\$1timestamp  |  timestamp  | 
|  quality  |  string  | 
|  解析度  |  string  | 
|  sum\$1value  |  double  | 
|  count\$1value  |  integer  | 
|  average\$1value  |  double  | 
|  maximum\$1value  |  double  | 
|  minimum\$1value  |  double  | 
|  stdev\$1value  |  double  | 

# 支援的資料類型
<a name="supported-data-types"></a>

AWS IoT SiteWise 查詢語言支援下列資料類型。


**純量值**  

|  **資料類型**  |  **Description**  | 
| --- | --- | 
|  `STRING`  |  長度上限為 1024 位元組的字串。  | 
|  `INTEGER`  |  帶正負號的 32 位元整數，範圍為 `-2,147,483,648 to 2,147,483,647` 。  | 
|  `DOUBLE`  |  範圍為 `–10^100 to 10^100`或`IEEE 754`雙精度`Nan`的浮點數。  | 
|  `BOOLEAN`  |  `true` 或 `false`  | 
|  `TIMESTAMP`  |  符合 ISO-8601 的時間戳記： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/supported-data-types.html)  | 

**注意**  
`Null`：指出`true`缺少已定義資料的布林值。

**Example**  
`TIMESTAMP` 值範例：  

```
TIMESTAMP '2025-12-21 23:59:59.999Z'
TIMESTAMP '2025-12-21 23:59:59+23:59'
'2025-12-21 23:59:59'
'2025-12-21T23:59:59.123+11:11'
```

**注意**  
 雙精確度資料並不精確。某些值不會完全轉換，而且由於精確度有限， 不會代表所有實際數字。查詢中的浮點資料可能與內部表示的值不同。如果輸入數字的精確度太高，則值會四捨五入。

# 支援的子句
<a name="supported-clauses"></a>

`SELECT` 陳述式用於從一或多個檢視擷取資料。 AWS IoT SiteWise 支援 `JOIN`和 `INNER JOIN`操作。

檢視會以明確的語法聯結，或在 `JOIN` `FROM`子句中以逗號分隔的符號聯結。

**Example**  
一般`SELECT`陳述式：  

```
SELECT expression [, ...]
  [ FROM table_name AS alias [, ...] ]
  [ WHERE condition ]
  [ GROUP BY expression [, ...] ]
  [ HAVING condition ]
  [ ORDER BY expression [ ASC | DESC ] [ NULLS FIRST | NULLS LAST ] [, ...] ]
  [ LIMIT expression ]
```

**Example**  
具有不同子句的 SELECT 陳述式：  

```
SELECT
  a.asset_name,
  a.asset_id,
  p.property_type,
  p.property_data_type,
  p.string_attribute_value,
  p.property_name
FROM asset a, asset_property p
WHERE a.asset_description LIKE '%description%'
AND p.property_type IN ('attribute', 'metric')
OR p.property_id IN (
  SELECT property_id
  FROM raw_time_series
  WHERE event_timestamp BETWEEN TIMESTAMP '2025-01-01 00:00:00' AND TIMESTAMP '2025-01-02 00:00:00'
  GROUP BY asset_id, property_id
  HAVING COUNT(*) > 100
 )
GROUP BY p.property_type
HAVING COUNT(*) > 5
ORDER BY a.asset_name ASC
LIMIT 20;
```

**注意**  
 隱含 `JOIN`會結合兩個或多個不同的資料表，而不根據 AWS IoT SiteWise內部結構描述使用`JOIN`關鍵字。這相當於`JOIN`在中繼資料`asset_id`和原始資料表之間的 和 `property_id` 欄位上執行 。此模式可讓 SiteWise 在從原始資料表擷取資料時，利用查詢中任何指定的中繼資料篩選條件，進而減少掃描的整體資料。  

**Example 查詢的 ：**  

```
SELECT a.asset_name, p.property_name, r.event_timestamp
FROM asset a, asset_property p, raw_time_series r
WHERE a.asset_name='my_asset' AND p.property_name='my_property'
```
上述範例只會從屬於指定中繼資料名稱的資產屬性掃描資料。  

**Example 的最佳化程度低於上述查詢：**  

```
SELECT a.asset_name, p.property_name, r.event_timestamp
FROM asset a
JOIN asset_property p ON a.asset_id=p.asset_id
JOIN raw_time_series r ON p.asset_id=r.asset_id AND p.property_id=r.property_id
WHERE a.asset_name='my_asset' AND p.property_name='my_property'
```

每個子句及其描述的說明如下所示：


|  **子句**  |  **Signature**  |  **Description**  | 
| --- | --- | --- | 
|  `LIMIT`  |  <pre>LIMIT { count }</pre>  |  此子句會將結果集限制為指定的資料列數。您可以使用 `LIMIT`搭配或不搭配 `ORDER BY`和 `OFFSET`子句。 `LIMIT` 僅適用於來自 【0，2147483647】 的非負整數。  | 
|  `ORDER BY`  |  <pre>ORDER BY expression<br />[ ASC | DESC ]<br />[ NULLS FIRST | NULLS LAST ] <br /></pre>  |  `ORDER BY` 子句會排序查詢的結果集。   在 `ORDER BY`子句的彙總中參考選取的資料欄時，請使用資料欄的順序索引，而不是名稱或別名。 <pre>SELECT AVG(t.double_value)<br />FROM latest_value_time_series t<br />GROUP BY t.asset_id<br />ORDER BY 1</pre>   | 
|  `GROUP BY`  |  <pre>GROUP BY expression [, ...]</pre>  |  `GROUP BY` 子句識別查詢的分組資料欄。它與彙總表達式搭配使用。  | 
|  `HAVING`  |  <pre>HAVING boolean-expression</pre>  |  `HAVING` 子句會篩選 GROUP BY 子句建立的群組資料列。  | 
|  `SUB SELECT`  |  <pre>SELECT column1, column2<br />FROM table1<br />WHERE column3 IN (SELECT column4 FROM table2);<br /></pre>  |  內嵌在另一個`SELECT`陳述式中的`SELECT`陳述式。  | 
|  `JOIN`  |  <pre>SELECT column1, column2<br />FROM table1 JOIN table2<br />ON table1.column1 = table2.column1;<br /></pre>  | 
|  `INNER JOIN`  |  <pre>SELECT columns<br />FROM table1<br />INNER JOIN table2 ON table1.column = table2.column;<br /></pre>  |  會`INNER JOIN`傳回兩個資料表中符合聯結條件的所有資料列。  | 
|  `UNION`  |  <pre>query<br />   { UNION [ ALL ] }<br />another_query<br /></pre>  |  `UNION` 運算子會計算其兩個引數的集合聯集，自動從結果集中移除重複的記錄。  | 

# 邏輯運算子
<a name="sql-supported-logical"></a>

AWS IoT SiteWise 支援下列邏輯運算子。


|  **運算子**  |  **Signature**  |  **Description**  | 
| --- | --- | --- | 
|  `AND`  |  a `AND` b  |  `TRUE` 如果兩個值都是 true  | 
|  `OR`  |  a `OR` b  |  `TRUE` 如果一個值為 true  | 
|  `NOT`  |  `NOT` 表達式  |  `TRUE` 如果表達式為 false，且`FALSE`如果表達式為 true  | 
|  `IN`  |  x `IN`表達式  |  `TRUE` 如果表達式中的值  | 
|  `BETWEEN`  |  `BETWEEN` a `AND` b  |  `TRUE` 如果值介於上限和下限之間，且包含兩個限制  | 
|  `LIKE`  |  `LIKE` 模式  |  `TRUE` 如果值處於模式 `LIKE` 支援萬用字元。如需範例，請參閱以下： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-supported-logical.html)  | 

所有邏輯運算子的範例：


|  **函數**  |  **範例**  | 
| --- | --- | 
|  AND  |  <pre>SELECT a.asset_name <br />   FROM asset AS a, latest_value_time_series AS t <br />   WHERE t.int_value > 30 AND t.event_timestamp > TIMESTAMP '2025-05-15 00:00:01'<br /></pre>  | 
|  或  |  <pre>SELECT a.asset_name <br />   FROM asset AS a<br />   WHERE a.asset_name like 'abc' OR a.asset_name like 'pqr'<br /></pre>  | 
|  NOT  |  <pre>SELECT ma.asset_id AS a_id<br />   FROM asset AS ma<br />   WHERE (ma.asset_id NOT LIKE 'some%patterna%' escape 'a') AND ma.asset_id='abc'<br /></pre>  | 
|  IN  |  <pre>SELECT a.asset_name <br />   FROM asset AS a<br />   WHERE a.asset_name IN ('abc', 'pqr')<br /></pre>  | 
|  BETWEEN  |  <pre>SELECT asset_id, int_value, event_timestamp AS i_v <br />   FROM raw_time_series<br />   WHERE event_timestamp BETWEEN TIMESTAMP '2025-04-15 00:00:01' and TIMESTAMP '2025-05-15 00:00:01'  <br /></pre>  | 
|  LIKE  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-supported-logical.html)  | 

# 比較運算子
<a name="sql-supported-comparision"></a>

AWS IoT SiteWise 支援下列比較運算子。所有比較操作都可用於內建資料類型，並評估為布林值。


**邏輯運算子**  

|  **運算子**  |  **Description**  | 
| --- | --- | 
|  `<`  |  Less than  | 
|  `>`  |  Greater than  | 
|  `<=`  |  小於或等於  | 
|  `>=`  |  大於或等於  | 
|  `=`  |  Equals  | 
|  `!=`  |  不等於  | 


**非數值的比較操作事實資料表**  

|  **類型**  |  **類型 >= x**  |  **類型 <= x**  |  **類型 > x**  |  **類型 < x**  |  **類型 = x**  |  **輸入 ！= x**  | 
| --- | --- | --- | --- | --- | --- | --- | 
|  `NULL`  |  `FALSE`  |  `FALSE`  |  `FALSE`  |  `FALSE`  |  `FALSE`  |  `TRUE`  | 

有些述詞的行為類似於運算子，但具有特殊語法。請參閱以下內容：


**比較述詞**  

|  **運算子**  |  **Description**  | 
| --- | --- | 
|  `IS NULL`  |  測試值是否為 `NULL`。  | 
|  `IS NOT NULL`  |  測試值是否不是 `NULL`。  | 

## NaN 運算子
<a name="sql-supported-comparision-nan"></a>

 `NaN`或 'Not a Number' 是浮點算術中的特殊值。以下是`NaN`比較清單及其運作方式。
+ `NaN` 值必須括在單引號內。例如，'`NaN`'。
+ `NaN` 值會被視為彼此相等。
+ `NaN` 大於其他數值。
+ 在彙總函數中`STDDEV()`，例如 `AVG()`、 和 `SUM()`，如果任何值為 `NaN`，則結果為 `NaN`。
+ 在彙總函數中，例如 `MAX()`和 `MIN()`，`NaN`值會包含在計算中。


**NaN 值比較**  

|  **Comparison** (比較)  |  **結果**  | 
| --- | --- | 
|  `'NaN' ≥ x`  |  True  | 
|  `'NaN' ≤ x`  |  如果 x 等於 NaN，則為 true，否則為 False  | 
|  `'NaN' > x`  |  如果 x 等於 NaN，則為 False，否則為 True  | 
|  `'NaN' < x`  |  False  | 
|  `'NaN' = x`  |  如果 x 等於 NaN，則為 true，否則為 False  | 
|  `'NaN' != x`  |  如果 x 等於 NaN，則為 False，否則為 True  | 

# SQL 函數
<a name="sql-functions"></a>

 支援的函數群組包括：

**Topics**
+ [純量函數](sql-functions-scalar.md)
+ [彙總函數](sql-functions-aggregated.md)

# 純量函數
<a name="sql-functions-scalar"></a>

 純量函數會取得一或多個輸入值，並傳回單一輸出值。它們廣泛用於 SQL （結構化查詢語言） 以進行資料處理和擷取，從而提高資料處理任務的效率。

**Topics**
+ [Null 資料函數](sql-functions-null.md)
+ [字串函數](sql-functions-string.md)
+ [數學函數](sql-functions-math.md)
+ [日期時間函數](sql-functions-date.md)
+ [類型轉換函數](sql-functions-type-conv.md)

# Null 資料函數
<a name="sql-functions-null"></a>

 Null 資料函數會處理或操作 NULL 值，這表示沒有值。函數可讓您將 NULLs 取代為其他值、檢查值是否為 NULL，或執行以特定方式處理 NULLs 的操作。


|  **函數**  |  **Signature**  |  **Description**  | 
| --- | --- | --- | 
|  `COALESCE`  |   COALESCE (expression1、 expression2、...、 expressionN)   |  如果所有表達式都評估為 null，則 COALESCE 會傳回 null。運算式必須為相同類型。  | 

**Example COALESCE 函數的**  

```
SELECT COALESCE (l.double_value, 100) AS non_double_value FROM latest_value_time_series AS l LIMIT 1
```

# 字串函數
<a name="sql-functions-string"></a>

 字串函數是用於操作和處理文字資料的內建工具。它們可在字串中啟用串連、擷取、格式化和搜尋等任務。這些函數對於清理、轉換和分析資料庫中以文字為基礎的資料至關重要。


**字串函數**  

|  **函數**  |  **Signature**  |  **Description**  | 
| --- | --- | --- | 
|  `LENGTH`  |   LENGTH （字串）   |  傳回字串的長度。  | 
|  `CONCAT`  |   CONCAT （字串、字串）   |  串連字串中的引數。  | 
|  `SUBSTR`  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-string.html)  |  傳回下列其中一項： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-string.html) 使用以 1 為基礎的索引做為啟動參數。  | 
|  `UPPER`  |   UPPER （字串）   |  將輸入字串中的字元轉換為大寫。  | 
|  `LOWER`  |   LOWER （字串）   |  將輸入字串中的字元轉換為小寫。  | 
|  `TRIM`  |   TRIM （字串）   |  從字串的開頭、結尾或兩側移除任何空格字元。  | 
|  `LTRIM`  |   LTRIM （字串）   |  從字串開頭移除任何空格字元。  | 
|  `RTRIM`  |   RTRIM （字串）   |  從字串結尾移除任何空格字元。  | 
|  `STR_REPLACE`  |   STR\$1REPLACE （字串、從、到）   |  以另一個指定的子字串取代指定子字串的所有出現次數。  | 

所有 函數的範例：


|  **函數**  |  **範例**  | 
| --- | --- | 
|  LENGTH  |  `SELECT LENGTH(a.asset_id) AS asset_id_length FROM asset AS a`  | 
|  CONCAT  |   `SELECT CONCAT(p.property_id, p.property_name) FROM asset_property AS p`   | 
|  SUBSTR  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-string.html)  | 
|  UPPER  |   `SELECT UPPER(d.string_value) AS up_string FROM raw_time_series AS d`   | 
|  LOWER  |   `SELECT LOWER(d.string_value) AS low_string FROM raw_time_series AS d`   | 
|  TRIM  |   `SELECT TRIM(d.string_value) AS tm_string FROM raw_time_series AS d`   | 
|  LTRIM  |   `SELECT LTRIM(d.string_value) AS ltrim_string FROM raw_time_series AS d`   | 
|  RTRIM  |   `SELECT RTRIM(d.string_value) AS rtrim_string FROM raw_time_series AS d`   | 
|  STR\$1REPLACE  |   `SELECT STR_REPLACE(d.string_value, 'abc', 'def') AS replaced_string FROM raw_time_series AS d`   | 

## 串連運算子
<a name="sql-operators-concatenation"></a>

 串連運算子 `||`或管道運算子將兩個字串聯結在一起。它提供 `CONCAT`函數的替代方案，並在結合多個字串時更容易讀取。

**Example 串連運算子的**  

```
SELECT a.asset_name || ' - ' || p.property_name 
  AS full_name
  FROM asset a, asset_property p
```

# 數學函數
<a name="sql-functions-math"></a>

 數學函數是 SQL 查詢中用來對數值資料執行計算的預先定義數學操作。它們提供操作和轉換資料的方法，而不需要從資料庫擷取資料並單獨處理資料。


**數學函數**  

|  **函數**  |  **Signature**  |  **Description**  | 
| --- | --- | --- | 
|  `POWER`  |  POWER (int\$1double、int\$1double)  |  傳回提升為第二個引數能力的第一個引數值。  | 
|  `ROUND`  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-math.html)  |  四捨五入至最接近的整數。  | 
|  `FLOOR`  |   FLOOR (int\$1double)   |  傳回不超過指定值的最大整數。  | 

所有 函數的範例：


|  **函數**  |  **範例**  | 
| --- | --- | 
|  POWER  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-math.html)  | 
|  ROUND  |   `ROUND (32.12435, 3)`   | 
|  FLOOR  |   `FLOOR (21.2)`   | 

# 日期時間函數
<a name="sql-functions-date"></a>

 日期時間函數適用於日期和時間。這些函數允許擷取日期的特定元件、執行計算和操作日期值。

這些函數中允許的識別符包括：
+ YEAR
+ MONTH
+ DAY
+ HOUR
+ MINUTE
+ SECOND


|  **函數**  |  **Signature**  |  **Description**  | 
| --- | --- | --- | 
|  `NOW`  |   現在 ( )   |  以毫秒精確度傳回目前的時間戳記。它提供在查詢中執行的確切時間。  | 
|  `DATE_ADD`  |  DATE\$1ADD （識別符、 interval\$1duration、資料欄）  |  傳回日期/時間和天數/小時的總和，或日期/時間和日期/時間間隔的總和。  | 
|  `DATE_SUB`  |  DATE\$1SUB （識別符、 interval\$1duration、資料欄）  |  傳回日期/時間和天數/小時之間的差異，或日期/時間和日期/時間間隔之間的差異。  | 
|  `TIMESTAMP_ADD`  |  TIMESTAMP\$1ADD （識別符、 interval\$1duration、資料欄）  |  將指定時間單位的時間間隔新增至日期時間表達式。  | 
|  `TIMESTAMP_SUB`  |  TIMESTAMP\$1SUB （識別符、 interval\$1duration、資料欄）  |  從日期時間表達式減去指定時間單位的時間間隔。  | 
|  `CAST`  |  CAST （表達式 AS TIMESTAMP FORMAT 模式）  |  使用指定的格式模式將字串表達式轉換為時間戳記。標準日期時間格式`'yyyy-MM-dd HH:mm:ss'`的常見模式包括 。例如 `SELECT CAST('2023-12-25 14:30:00' AS TIMESTAMP) AS converted_timestamp`  | 

**Example 使用所列函數的 SQL 查詢：**  

```
SELECT r.asset_id, r.int_value,
  date_add(DAY, 7, r.event_timestamp) AS date_in_future,
  date_sub(YEAR, 2, r.event_timestamp) AS date_in_past,
  timestamp_add(DAY, 2, r.event_timestamp) AS timestamp_in_future,
  timestamp_sub(DAY, 2, r.event_timestamp) AS timestamp_in_past,
  now() AS time_now
FROM raw_time_series AS r
```

# 類型轉換函數
<a name="sql-functions-type-conv"></a>

 類型轉換函數用於將值的資料類型從一個變更為另一個。它們對於確保資料相容性和執行需要特定格式資料的操作至關重要。


|  **函數**  |  **Signature**  |  **Description**  | 
| --- | --- | --- | 
|  `TO_DATE`  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  | 
|  `TO_TIMESTAMP`  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  | 
|  `TO_TIME`  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  | 
|  `CAST`  |  CAST (<expression> AS <data type>)  |  將評估為單一值的實體或表達式，從一種類型轉換為另一種類型。 支援的資料類型如下： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-type-conv.html)  | 

**Example 使用所列函數的 SQL 查詢：**  

```
SELECT TO_TIMESTAMP (100) AS timestamp_value,
  TO_DATE(r.event_timestamp) AS date_value,
  TO_TIME(r.event_timestamp) AS time_value
FROM raw_time_series AS r
```

# 彙總函數
<a name="sql-functions-aggregated"></a>

 彙總函數是跨多資料列執行計算的資料庫操作，可產生單一摘要結果。這些函數會分析資料集，以傳回計算值，例如總和、平均值、計數或其他統計指標。


|  **函數**  |  **Signature**  |  **Description**  | 
| --- | --- | --- | 
|  `AVG`  |  AVG （表達式）  |  傳回數值表達式的平均值。  | 
|  `COUNT`  |  COUNT （表達式）  |  傳回符合指定條件的資料列數。  | 
|  `MAX`  |  MAX （表達式）  |  傳回所選表達式的最大值。  | 
|  `MIN`  |  MIN （表達式）  |  傳回所選表達式的最小值。  | 
|  `SUM`  |  SUM （表達式）  |  傳回數值表達式的總和。  | 
|  `STDDEV`  |  STDDEV （表達式）  |  傳回範例標準差。  | 
|  `GROUP BY`  |  GROUP BY 運算式  |  傳回由分組資料欄建立的資料列。  | 
|  `HAVING`  |  HAVING 布林值表達式  |  傳回依`GROUP BY`子句篩選的群組資料列。  | 

所有 函數的範例：


|  **函數**  |  **範例**  | 
| --- | --- | 
|  AVG  |  <pre>SELECT d.asset_id, d.property_id, AVG(d.int_value) FROM raw_time_series AS d</pre>  | 
|  COUNT  |  <pre>SELECT COUNT(d.int_value) FROM raw_time_series AS d</pre>  | 
|  MAX  |  <pre>SELECT MAX(d.int_value) FROM raw_time_series AS d</pre>  | 
|  MIN  |  <pre>SELECT MIN(d.int_value) FROM raw_time_series AS d</pre>  | 
|  SUM  |  <pre>SELECT SUM(d.int_value) FROM raw_time_series AS d</pre>  | 
|  STDDEV  |  <pre>SELECT STDDEV(d.int_value) FROM raw_time_series AS d</pre>  | 
|  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/iot-sitewise/latest/userguide/sql-functions-aggregated.html)  |  <pre>SELECT MAX(d.int_value) AS max_int_value, d.asset_id <br />FROM raw_time_series AS d <br />GROUP BY d.asset_id <br />HAVING MAX(d.int_value) > 5                      <br /></pre>  | 

# 查詢範例
<a name="sql-examples"></a>

## 中繼資料篩選
<a name="sql-examples-meta-filter"></a>

下列範例適用於使用 AWS IoT SiteWise 查詢語言的 `SELECT`陳述式進行中繼資料篩選：

```
SELECT a.asset_name, p.property_name
FROM asset a, asset_property p
WHERE a.asset_name LIKE 'Windmill%'
```

## 值篩選
<a name="sql-examples-value-filter"></a>

以下是使用具有 AWS IoT SiteWise 查詢語言的`SELECT`陳述式篩選值的範例：

```
SELECT a.asset_name, r.int_value
FROM asset a, raw_time_series r
WHERE r.int_value > 30
AND r.event_timestamp > TIMESTAMP '2022-01-05 12:15:00'
AND r.event_timestamp < TIMESTAMP '2022-01-05 12:20:00'
```