# Window - 指令碼函數

Window() 從多列執行計算，為每列分別產生值。

您可以使用 Window 函數執行操作，例如：

* 比較列中的個別數值與欄內的平均值、最大值或最小值。

* 計算個別值的排名，欄內或整個表格內皆可。

Window 函數不會變更表格中的記錄數量，但仍然可以執行與彙總、關係和範圍函數類似的任務。

Window 函數在您所用表格的 LOAD 陳述式內必須有快取，以新增至表格。例如︰

```
[Transactions]:
Load 
	*,
	Window(avg(Expression1),[Num]);
LOAD
	TransLineID,
	TransID,
	"Num",
	Dim1,
	Dim2,
	Dim3,
	Expression1,
	Expression2,
	Expression3
FROM [lib://DataFiles/transactions.qvd] (qvd);FROM [lib://AttachedFiles/transactions.qvd] (qvd);
```

Window 支援一般函數，例如捨入或基本數字操作。例如︰

```
Load *, Round(Window(Sum(Salary),Department)) as SumSalary
```

```
Load *, Window(Sum(Salary),Department) + 5 as SumSalary
```

您可以定義 Window 函數的滑動視窗。這設定在目前的列套用 Window 函數時使用的列數。例如，您可以將視窗設定為之前 3 列和之後 3 列：

Window(avg(Salary), Department, 'NONE' -3, 3) as AvgDepSalary

語法：

Window (Window (input\_expr, [partition1, partition2, ...], [sort\_type, [sort\_expr]],[filter\_expr], [start\_expr,end\_expr]))

傳回資料類型：新欄位新增至透過 LOAD 陳述式建立的結果表格。

引數：

引數

| 引數 | 描述 |
| --- | --- |
| input\_expr | 函數計算並傳回的輸入運算式。這必須是基於彙總的任何運算式，例如 Median(Salary)。例如︰<br><br>Window(Median(Salary)) as MedianSalary<br><br>輸入也可以是沒有套用彙總的欄位名稱。在此情況下，Window 的處理方式如同 Only() 函數套用至該欄位。例如︰<br><br>Window(Salary,Department) as WSalary<br><br>您也可以選擇透過輸入運算式定義分割。分割與透過 group by 子句達成的分組相同，差異在於結果是作為新的欄新增至輸入表格。分割不會減少輸入表格的記錄數量。可以定義多個分割區欄位。<br><br>範例：<br><br><br>LOAD<br>Window(Max(Sales), City, 'ASC', OrderDate, Sales &gt; 300)<br>+ AddMonths(OrderDate,-6) as MAX\_Sales\_City\_Last\_6\_Mos,<br>Window(Avg(Sales), City, 'ASC', OrderDate, City = 'Portland')<br>+ AddMonths(OrderDate,-6) as Avg\_Sales\_Portland\_Last\_6\_Mos,<br>Window(Max(Sales), City, 'ASC', OrderDate, Sales &gt; 300)<br>+ AddMonths(OrderDate,-12) as MAX\_Sales\_City\_Last\_12\_Mos;<br>LOAD<br>City,<br>Sales,<br>OrderDate<br>FROM [lib://:DataFiles/Sales Data.xlsx]FROM [lib://AttachedFiles/Sales Data.xlsx]<br>(ooxml, embedded labels, table is [Sales Data]); |
| partition1, partition2 | 在 input\_expr 之後，您可以定任何分割區數量。分割是定義哪些組合要套用彙總的欄位。會在每個分割分別套用彙總。例如︰<br><br>Window(Avg(Salary), Unit, Department, Country) as AvgSalary<br><br>如上述，分割為 Unit、Department 和 Country。<br><br>分割不是強制性質，但需要分割才能正常建立欄位視窗。 |
| sort\_type, [sort\_expr]] | 也可以選擇指定排序類型和排序運算式。sort\_type 可以有兩個值之一：<br><br><ul><li><p><span>ASC</span>：&nbsp;遞增排序。</p></li><li><p><span>DESC</span>：&nbsp;遞減排序。</p></li></ul><br><br>若您定義 sort\_type，您需要定義排序運算式。此為決定分割內列順序的運算式。<br><br>例如︰<br><br>Window(RecNo(), Department, 'ASC', Year)<br><br>在以上範例中，呈現分割內透過 Year 欄位遞增排序的結果。<br><br>資訊備註排序類型和排序運算式主要僅在搭配 RecNo 和 WRank 時需要。 |
| filter\_expr | 也可以選擇新增篩選運算式。此為決定是否應在計算中納入記錄的布林運算式。<br><br>可以完全省略此參數，而結果應為沒有任何篩選條件。<br><br>例如︰<br><br>Window(avg(Salary), Department, 'ASC', Age, EmployeeID=3 Or EmployeeID=7)<br>as wAvgSalary) as wAvgSalaryIfEmpIs3or7 |
| [start\_expr,end\_expr] | 也可以選擇為滑動視窗功能設定引數。 滑動視窗需要兩個引數：<br><br><ul><li><p>開始運算式：&nbsp;要在視窗中納入的目前列之前的列數。</p></li><li><p>結束運算式：&nbsp;要在視窗中納入的目前列之後的列數。</p></li></ul><br><br>例如，若您想要納入之前 3 列、目前列和下一列：<br><br>Window(concat(Text(Salary),'-'), Department, 'ASC', Age, Year&gt;0, -3, 1)<br>as WSalaryDepartment<br><br>若要指示所有之前的列或所有之後的列，您可以使用 Unbounded() 函數。例如，若要納入所有之前的列、目前的列以及下一列：<br><br>Window(concat(Text(Salary),'-'), Department, 'ASC', Age, Year&gt;0, UNBOUNDED(), 1)<br>as WSlidingSalaryDepartment<br><br>例如，若要納入從目前的列算起第三列和所有之後的列：<br><br>Window(concat(Text(Salary),'-'), Department, 'ASC', Age, Year&gt;0, 3, UNBOUNDED())<br>as WSlidingSalaryDepartment |

## 範例 - 新增包含彙總的欄位

[](https://help.qlik.com/en-US/qlikview/Subsystems/Client/Content/QV_QlikView/Scripting/WindowFunctions/window.htm)

#### 範例：新增包含彙總的欄位

載入指令碼

在資料載入編輯器中建立新的索引標籤，然後載入下列資料作為內嵌載入。在 Qlik Sense 中建立以下表格以查看結果。

```
Transactions:
Load
*,
Window(Avg(transaction_amount),customer_id) as AvgCustTransaction;

Load * Inline [
transaction_id, transaction_date, transaction_amount, transaction_quantity, customer_id, size, color_code
3750, 20180830, 23.56, 2, 2038593, L, Red
3751, 20180907, 556.31, 6, 203521, M, Orange
3752, 20180916, 5.75, 1, 5646471, S, Blue
3753, 20180922, 125.00, 7, 3036491, L, Black
3754, 20180922, 484.21, 13, 049681, XS, Red
3756, 20180922, 59.18, 2, 2038593, M, Blue
3757, 20180923, 177.42, 21, 203521, XL, Black
3758, 20180924, 153.42, 14, 2038593, L, Red
3759, 20180925, 7.42, 5, 203521, M, Orange
3760, 20180925, 80.12, 18, 5646471, M, Blue
3761, 20180926, 3.42, 7, 3036491, XS, Black
3763, 20180926, 63.55, 12, 049681, S, Red
3763, 20180927, 177.56, 10, 2038593, L, Blue
3764, 20180927, 325.95, 8, 203521, XL, Black
];
 
```

結果

新增包含彙總的欄位的結果

| transaction\_id | transaction\_date | transaction\_amount | transaction\_quantity | customer\_id | 大小 | color\_code | AvgCustTransaction |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 3750 | 20180830 | 23.56 | 2 | 2038593 | L | 紅色 | 103.43 |
| 3751 | 20180907 | 556.31 | 6 | 203521 | M | 橘色 | 266.775 |
| 3752 | 20180916 | 5.75 | 1 | 5646471 | S | 藍色 | 42.935 |
| 3753 | 20180922 | 125.00 | 7 | 3036491 | L | 黑色 | 64.21 |
| 3754 | 20180922 | 484.21 | 13 | 049681 | XS | 紅色 | 273.88 |
| 3756 | 20180922 | 59.18 | 2 | 2038593 | M | 藍色 | 103.43 |
| 3757 | 20180923 | 177.42 | 21 | 203521 | XL | 黑色 | 266.775 |
| 3758 | 20180924 | 153.42 | 14 | 2038593 | L | 紅色 | 103.43 |
| 3759 | 20180925 | 7.42 | 5 | 203521 | M | 橘色 | 266.775 |
| 3760 | 20180925 | 80.12 | 18 | 5646471 | M | 藍色 | 42.935 |
| 3761 | 20180926 | 3.42 | 7 | 3036491 | XS | 黑色 | 64.21 |
| 3763 | 20180926 | 63.55 | 12 | 049681 | S | 紅色 | 273.88 |
| 3763 | 20180927 | 177.56 | 10 | 2038593 | L | 藍色 | 103.43 |
| 3764 | 20180927 | 325.95 | 8 | 203521 | XL | 黑色 | 266.775 |

## 範例 - 新增包含為特定值篩選之彙總的欄位

[](https://help.qlik.com/en-US/qlikview/Subsystems/Client/Content/QV_QlikView/Scripting/WindowFunctions/window.htm)

#### 範例：新增包含為特定值篩選之彙總的欄位

載入指令碼

在資料載入編輯器中建立新的索引標籤，然後載入下列資料作為內嵌載入。在 Qlik Sense 中建立以下表格以查看結果。

```
Transactions:
Load
*,
Window(Avg(transaction_amount),customer_id, color_code = 'Blue') as AvgCustTransaction;

Load * Inline [
transaction_id, transaction_date, transaction_amount, transaction_quantity, customer_id, size, color_code
3750, 20180830, 23.56, 2, 2038593, L, Red
3751, 20180907, 556.31, 6, 203521, M, Orange
3752, 20180916, 5.75, 1, 5646471, S, Blue
3753, 20180922, 125.00, 7, 3036491, L, Black
3754, 20180922, 484.21, 13, 049681, XS, Red
3756, 20180922, 59.18, 2, 2038593, M, Blue
3757, 20180923, 177.42, 21, 203521, XL, Black
3758, 20180924, 153.42, 14, 2038593, L, Red
3759, 20180925, 7.42, 5, 203521, M, Orange
3760, 20180925, 80.12, 18, 5646471, M, Blue
3761, 20180926, 3.42, 7, 3036491, XS, Black
3763, 20180926, 63.55, 12, 049681, S, Red
3763, 20180927, 177.56, 10, 2038593, L, Blue
3764, 20180927, 325.95, 8, 203521, XL, Black
];
 
```

結果

新增包含為特定值篩選之彙總的欄位的結果

| transaction\_id | transaction\_date | transaction\_amount | transaction\_quantity | customer\_id | 大小 | color\_code | AvgCustTransaction |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 3750 | 20180830 | 23.56 | 2 | 2038593 | L | 紅色 | - |
| 3751 | 20180907 | 556.31 | 6 | 203521 | M | 橘色 | - |
| 3752 | 20180916 | 5.75 | 1 | 5646471 | S | 藍色 | 42.94 |
| 3753 | 20180922 | 125.00 | 7 | 3036491 | L | 黑色 | - |
| 3754 | 20180922 | 484.21 | 13 | 049681 | XS | 紅色 | - |
| 3756 | 20180922 | 59.18 | 2 | 2038593 | M | 藍色 | 118.4 |
| 3757 | 20180923 | 177.42 | 21 | 203521 | XL | 黑色 | - |
| 3758 | 20180924 | 153.42 | 14 | 2038593 | L | 紅色 | - |
| 3759 | 20180925 | 7.42 | 5 | 203521 | M | 橘色 | - |
| 3760 | 20180925 | 80.12 | 18 | 5646471 | M | 藍色 | 42.94 |
| 3761 | 20180926 | 3.42 | 7 | 3036491 | XS | 黑色 | - |
| 3763 | 20180926 | 63.55 | 12 | 049681 | S | 紅色 | - |
| 3763 | 20180927 | 177.56 | 10 | 2038593 | L | 藍色 | 118.4 |
| 3764 | 20180927 | 325.95 | 8 | 203521 | XL | 黑色 | - |

## 範例 - 新增具有滑動視窗的欄位

[](https://help.qlik.com/en-US/qlikview/Subsystems/Client/Content/QV_QlikView/Scripting/WindowFunctions/window.htm)

#### 範例：新增具有滑動視窗的欄位

載入指令碼

在資料載入編輯器中建立新的索引標籤，然後載入下列資料作為內嵌載入。在 Qlik Sense 中建立以下表格以查看結果。

```
Transactions:
Load
*,
Window(Avg(transaction_amount),customer_id, 'ASC', -1, 1, 0, 1) as AvgCustTransaction;

Load * Inline [
transaction_id, transaction_date, transaction_amount, transaction_quantity, customer_id, size, color_code
3750, 20180830, 23.56, 2, 2038593, L, Red
3751, 20180907, 556.31, 6, 203521, M, Orange
3752, 20180916, 5.75, 1, 5646471, S, Blue
3753, 20180922, 125.00, 7, 3036491, L, Black
3754, 20180922, 484.21, 13, 049681, XS, Red
3756, 20180922, 59.18, 2, 2038593, M, Blue
3757, 20180923, 177.42, 21, 203521, XL, Black
3758, 20180924, 153.42, 14, 2038593, L, Red
3759, 20180925, 7.42, 5, 203521, M, Orange
3760, 20180925, 80.12, 18, 5646471, M, Blue
3761, 20180926, 3.42, 7, 3036491, XS, Black
3763, 20180926, 63.55, 12, 049681, S, Red
3763, 20180927, 177.56, 10, 2038593, L, Blue
3764, 20180927, 325.95, 8, 203521, XL, Black
];
 
```

結果

新增包含為特定值篩選之彙總的欄位的結果

| transaction\_id | transaction\_date | transaction\_amount | transaction\_quantity | customer\_id | 大小 | color\_code | AvgCustTransaction |
| --- | --- | --- | --- | --- | --- | --- | --- |
| 3750 | 20180830 | 23.56 | 2 | 2038593 | L | 紅色 | 41.37 |
| 3751 | 20180907 | 556.31 | 6 | 203521 | M | 橘色 | 366.865 |
| 3752 | 20180916 | 5.75 | 1 | 5646471 | S | 藍色 | 42.935 |
| 3753 | 20180922 | 125.00 | 7 | 3036491 | L | 黑色 | 64.21 |
| 3754 | 20180922 | 484.21 | 13 | 049681 | XS | 紅色 | 273.88 |
| 3756 | 20180922 | 59.18 | 2 | 2038593 | M | 藍色 | 106.3 |
| 3757 | 20180923 | 177.42 | 21 | 203521 | XL | 黑色 | 92.42 |
| 3758 | 20180924 | 153.42 | 14 | 2038593 | L | 紅色 | 165.49 |
| 3759 | 20180925 | 7.42 | 5 | 203521 | M | 橘色 | 166.685 |
| 3760 | 20180925 | 80.12 | 18 | 5646471 | M | 藍色 | 80.12 |
| 3761 | 20180926 | 3.42 | 7 | 3036491 | XS | 黑色 | 3.42 |
| 3763 | 20180926 | 63.55 | 12 | 049681 | S | 紅色 | 177.56 |
| 3763 | 20180927 | 177.56 | 10 | 2038593 | L | 藍色 | 63.55 |
| 3764 | 20180927 | 325.95 | 8 | 203521 | XL | 黑色 | 325.95 |

## 限制

Window 有下列限制：

* Window 是資源密集函數，尤其是在記憶體取用方面。

* Qlik Sense Mobile 中不支援 Window。

* 圖表運算式不支援 Window。

* 您無法在其他 Window 函數內部內嵌 Window 函數。

* 無法在彙總函數內部使用 Window。

* Window 需要能夠掃描整個表格。

* 使用滑動視窗功能時，WRank()、RecNo() 和 RowNo() 無法搭配 Window 使用。