# Window - 指令碼函數

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

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

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

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

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

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

```
[Transactions]:
Load 
	*,
	Window(avg(Expression1),[Num]);
LOAD
	TransLineID,
	TransID,
	"Num",
	Dim1,
	Dim2,
	Dim3,
	Expression1,
	Expression2,
	Expression3
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 (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://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/sense/Subsystems/Hub/Content/Sense_Hub/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/sense/Subsystems/Hub/Content/Sense_Hub/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/sense/Subsystems/Hub/Content/Sense_Hub/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。