---
schema_version: "1"
id: ffcb7a262f8e3a9499af7aed9ab6adbda0ae7605fef4f4a9e7e3c96e7fa73984
title: 直通函數 (RAWSQL)
collection: tableau
version: current
locale: zh-tw
portal_url: https://docs.salesforce.com/tableau/current/zh-tw/pro/desktop/functions_functions_passthrough.md
canonical_url: https://help.tableau.com/current/pro/desktop/zh-tw/functions_functions_passthrough.htm
source_site: help.tableau.com
content_type: documentation
content_hash: fe9015e78cfa36baa5cd777c7b0e11c04fcca1d7092bc76dd388eb016f949e6b
description: Reference for RAWSQL pass-through functions that send SQL directly to the database.
---
[跳至主要內容](#main-content)

# 直通函數 (RAWSQL)

這些 RAWSQL 直通函數直接將 SQL 運算式傳送到資料庫，而無需先由 Tableau 解譯。如果您有 Tableau 不能識別的自訂資料庫函數，則可以使用直通函式呼叫這些自訂函數。

由於 Tableau 不會解譯運算式，因此必須在必要時定義彙總。需要傳遞彙總運算式時，可以使用函數的 `RAWSQLAGG` 版本。

RAWSQL 直通函數可能不適用於同盟（跨不同資料庫合併）或已發佈資料來源。

## RAWSQL 語法

RAWSQL 函數有兩種類型，分解函數和彙總函數。這在函數名稱的第一部分 _RAWSQL_ 或 _RAWSQLAGG_ 中指定。函數名稱的最後一部分是輸出類型，例如 _BOOL_、_STR_ 或 _INT_。在所有 RAWSQL 函數中，引數都為 `"sql_expr", [arg1], ...[arg2]`。編寫函數時，可以使用替代語法 `%n` 插入正確的欄位名稱或運算式。

### %n 替代語法

您的資料庫不會理解在 Tableau 中顯示的欄位名稱。因為 Tableau 不會解釋在直通函數中的 SQL 運算式，所以在運算式中使用 Tableau 欄位名稱可能會導致錯誤。使用 `%n` 將 Tableau 計算的正確欄位名稱或運算式插入傳遞 SQL 中。

例如，假設您有一個計算一組中值的函數，則可以對 Tableau 資料欄 \[Sales\] 呼叫該函數，如下所示：

RAWSQLAGG\_REAL("MEDIAN(%1)", \[Sales\])

- `REALSQLAGG` 因為您想要指定彙總。
- `REAL` 因為輸出是數字，不一定是整數。
- `MEDIAN` 是彙總。
- `%1` 是 `[Sales]` 的預留位置。

## RAWSQL 函數

SQL 運算式直接傳遞給基礎資料庫。在 SQL 運算式中將 %n 用作資料庫值的替換語法。

Tableau 中提供了以下 RAWSQL 函數：

### RAWSQL\_BOOL

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"><col></colgroup><tbody><tr><td>語法</td><td><code>RAWSQL_BOOL("sql_expr", [arg1], …[argN])</code></td></tr><tr><td>輸出</td><td>布林值</td></tr><tr><td>定義</td><td>從給定 SQL 運算式返回布林結果。</td></tr><tr><td>範例</td><td><pre xml:space="preserve">RAWSQL_BOOL("%1 &gt; %2", [Sales], [Profit])</pre><p>在範例中，%1 等於 [Sales]，%2 等於 [Profit]。</p></td></tr></tbody></table>

### RAWSQLAGG\_BOOL

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"><col></colgroup><tbody><tr><td>語法</td><td><code>RAWSQLAGG_BOOL("sql_expr", [arg1], …[argN])</code></td></tr><tr><td>輸出</td><td>布林值</td></tr><tr><td>定義</td><td>從給定彙總 SQL 運算式返回布林結果。</td></tr><tr><td>範例</td><td><pre xml:space="preserve">RAWSQLAGG_BOOL("SUM( %1) &gt;SUM( %2)", [Sales], [Profit]) </pre><p>在範例中，%1 等於 [Sales]，%2 等於 [Profit]。</p></td></tr></tbody></table>

### RAWSQL\_DATE

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"><col></colgroup><tbody><tr><td>語法</td><td><code>RAWSQL_DATE("sql_expr", [arg1], …[argN])</code></td></tr><tr><td>輸出</td><td>Date（日期）</td></tr><tr><td>定義</td><td>從給定 SQL 運算式傳回日期結果。</td></tr><tr><td>範例</td><td><pre xml:space="preserve">RAWSQL_DATE("%1", [Order Date])</pre><p>在下例中，%1 等於 [Order Date]。</p></td></tr></tbody></table>

### RAWSQLAGG\_DATE

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"><col></colgroup><tbody><tr><td>語法</td><td><code>RAWSQLAGG_DATE("sql_expr", [arg1], …[argN])</code></td></tr><tr><td>輸出</td><td>Date（日期）</td></tr><tr><td>定義</td><td>從給定彙總 SQL 運算式傳回日期結果</td></tr><tr><td>範例</td><td><pre xml:space="preserve">RAWSQLAGG_DATE("MAX(%1)", [Order Date])</pre><p>在下例中，%1 等於 [Order Date]。</p></td></tr></tbody></table>

### RAWSQL\_DATETIME

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"><col></colgroup><tbody><tr><td>語法</td><td><code>RAWSQL_DATETIME("sql_expr", [arg1], …[argN])</code></td></tr><tr><td>輸出</td><td>日期時間</td></tr><tr><td>定義</td><td>從給定 SQL 運算式傳回日期時間結果。</td></tr><tr><td>範例</td><td><pre xml:space="preserve">RAWSQL_DATETIME("%1", [Order Date])</pre><p>在下例中，%1 等於 [Order Date]。</p></td></tr></tbody></table>

### RAWSQLAGG\_DATETIME

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"><col></colgroup><tbody><tr><td>語法</td><td><code>RAWSQLAGG_DATETIME("sql_expr", [arg1], …[argN])</code></td></tr><tr><td>輸出</td><td>日期時間</td></tr><tr><td>定義</td><td>從給定彙總 SQL 運算式傳回日期時間結果。</td></tr><tr><td>範例</td><td><pre xml:space="preserve">RAWSQLAGG_DATETIME("MIN(%1)", [Order Date])</pre><p>在下例中，%1 等於 [Order Date]。</p></td></tr></tbody></table>

### RAWSQL\_INT

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"><col></colgroup><tbody><tr><td>語法</td><td><code>RAWSQL_INT("sql_expr", [arg1], …[argN])</code></td></tr><tr><td>輸出</td><td>整數</td></tr><tr><td>定義</td><td>從給定 SQL 運算式返回整數結果。</td></tr><tr><td>範例</td><td><pre xml:space="preserve">RAWSQL_INT("500 + %1", [Sales])</pre><p>在下例中，%1 等於 [Sales]。</p></td></tr></tbody></table>

### RAWSQLAGG\_INT

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"><col></colgroup><tbody><tr><td>語法</td><td><code>RAWSQLAGG_INT("sql_expr", [arg1,] …[argN])</code></td></tr><tr><td>輸出</td><td>整數</td></tr><tr><td>定義</td><td>從給定彙總 SQL 運算式返回整數結果。</td></tr><tr><td>範例</td><td><pre xml:space="preserve">RAWSQLAGG_INT("500 + SUM(%1)", [Sales])</pre><p>在下例中，%1 等於 [Sales]。</p></td></tr></tbody></table>

### RAWSQL\_REAL

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"><col></colgroup><tbody><tr><td>語法</td><td><code>RAWSQL_REAL("sql_expr", [arg1], …[argN])</code></td></tr><tr><td>輸出</td><td>數字</td></tr><tr><td>定義</td><td>從給定 SQL 運算式傳回數字結果。</td></tr><tr><td>範例</td><td><pre>RAWSQL_REAL("-123.98 * %1", [Sales])</pre><p>在下例中，%1 等於 [Sales]</p></td></tr></tbody></table>

### RAWSQLAGG\_REAL

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"><col></colgroup><tbody><tr><td>語法</td><td><code>RAWSQLAGG_REAL("sql_expr", [arg1,] …[argN])</code></td></tr><tr><td>輸出</td><td>數字</td></tr><tr><td>定義</td><td>從給定彙總 SQL 運算式傳回數字結果。</td></tr><tr><td>範例</td><td><pre xml:space="preserve">RAWSQLAGG_REAL("SUM( %1)", [Sales])</pre><p>在下例中，%1 等於 [Sales]。</p></td></tr></tbody></table>

### RAWSQL\_SPATIAL

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"><col></colgroup><tbody><tr><td>語法</td><td><code>RAWSQL_SPATIAL("sql_expr", [arg1], …[argN])</code></td></tr><tr><td>輸出</td><td>空間</td></tr><tr><td>定義</td><td>從給定 SQL 運算式傳回空間結果。</td></tr><tr><td>範例</td><td><pre xml:space="preserve">RAWSQL_SPATIAL("%1", [Geometry])</pre><p>在本例中，%1 等於 [Geometry]。</p></td></tr><tr><td>附註</td><td>此函數沒有 <code>RAWSQLAGG</code> 版本。</td></tr></tbody></table>

### RAWSQL\_STR

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"><col></colgroup><tbody><tr><td>語法</td><td><code>RAWSQL_STR("sql_expr", [arg1], …[argN])</code></td></tr><tr><td>輸出</td><td>字串</td></tr><tr><td>定義</td><td>從給定 SQL 運算式傳回字串。</td></tr><tr><td>範例</td><td><pre xml:space="preserve">RAWSQL_STR("%1", [Customer Name])</pre><p>在下例中，%1 等於 [Customer Name]。</p></td></tr></tbody></table>

### RAWSQLAGG\_STR

<table class="table--plain" style="width: 100%;"><colgroup><col style="width: 20%;"><col></colgroup><tbody><tr><td>語法</td><td><code>RAWSQLAGG_STR("sql_expr", [arg1,] …[argN])</code></td></tr><tr><td>輸出</td><td>字串</td></tr><tr><td>定義</td><td>從給定彙總 SQL 運算式傳回字串。</td></tr><tr><td>範例</td><td><pre xml:space="preserve">RAWSQLAGG_STR("AVG(%1)", [Discount])</pre><p>在此範例中，%1 等於 [Discount]。</p></td></tr></tbody></table>