

 Amazon Redshift 將不再支援從修補程式 198 開始建立新的 Python UDFs。現有 Python UDF 將繼續正常運作至 2026 年 6 月 30 日。如需詳細資訊，請參閱[部落格文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

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

# COUNT 範圍函數
<a name="r_WF_COUNT"></a>

 COUNT 範圍函數會計算表達式所定義的列數。

COUNT 函數有兩種版本。COUNT(\*) 計算目標資料表中的所有列數，而不論是否包含 Null。COUNT (表達式) 計算特定欄或表達式中不含 NULL 值的列數。

## 語法
<a name="r_WF_COUNT-synopsis"></a>

```
COUNT ( * | [ ALL ] expression) OVER
(
[ PARTITION BY expr_list ]
[ ORDER BY order_list 
                        frame_clause ]
)
```

## 引數
<a name="r_WF_COUNT-arguments"></a>

 *expression *   
函數運算的目標欄或表達式。

ALL   
如果指定引數 ALL，則函數在計數時會保留表達式中的所有重複值。ALL 為預設值。不支援 DISTINCT。

OVER   
指定彙總函數的視窗子句。OVER 子句區分視窗彙總函數和正常組彙總函數。

PARTITION BY *expr\_list*   
以一或多個表達式定義 COUNT 函數的視窗。

ORDER BY *order\_list*   
排序每一個分割區內的列。如果未指定 PARTITION BY，ORDER BY 會使用整個資料表。

 *frame\_clause*   
如果彙總函數使用 ORDER BY 子句，則需要明確的窗框子句。窗框子句在排序的結果內包含或排除列集，以調整函數視窗中的一個列集。窗框子句包含 ROWS 關鍵字和相關的指定元。請參閱 [範圍函數語法摘要](c_Window_functions.md#r_Window_function_synopsis)。

## 資料類型
<a name="c_Supported_data_types_wf_count"></a>

COUNT 函數支援所有引數資料類型。

COUNT 函數支援的傳回類型為 BIGINT。

## 範例
<a name="r_WF_COUNT-examples"></a>

 下列範例顯示從資料視窗開頭的所有列的銷售 ID、數量和計數：

```
select salesid, qty,
count(*) over (order by salesid rows unbounded preceding) as count
from winsales
order by salesid;

salesid | qty | count
---------+-----+-----
10001 |  10 |   1
10005 |  30 |   2
10006 |  10 |   3
20001 |  20 |   4
20002 |  20 |   5
30001 |  10 |   6
30003 |  15 |   7
30004 |  20 |   8
30007 |  30 |   9
40001 |  40 |   10
40005 |  10 |   11
(11 rows)
```

如需 WINSALES 資料表的描述，請參閱[範圍函數範例的範例資料表](c_Window_functions.md#r_Window_function_example)。

下列範例顯示如何從資料視窗開頭計算非 null 列的銷售 ID、數量和計數。(在 WINSALES 資料表中，QTY\_SHIPPED 欄包含一些 NULL。) 

```
select salesid, qty, qty_shipped,
count(qty_shipped)
over (order by salesid rows unbounded preceding) as count
from winsales
order by salesid;

salesid | qty | qty_shipped | count
---------+-----+-------------+-------
10001 |  10 |          10 |   1
10005 |  30 |             |   1
10006 |  10 |             |   1
20001 |  20 |          20 |   2
20002 |  20 |          20 |   3
30001 |  10 |          10 |   4
30003 |  15 |             |   4
30004 |  20 |             |   4
30007 |  30 |             |   4
40001 |  40 |             |   4
40005 |  10 |          10 |   5
(11 rows)
```