# 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>：升序排序。</p></li><li><p><span>DESC</span>：降序排序。</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>开始表达式：要包含在窗口中的当前行之前的行数。</p></li><li><p>结束表达式：要包含在窗口中的当前行之后的行数。</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 | CustomerID | 大小 | 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 | CustomerID | 大小 | 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 | CustomerID | 大小 | 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 是一个资源密集型函数，特别是在内存消耗方面。

* Window 在 Qlik Sense Mobile 中不受支持。

* 图表表达式不支持Window。

* 不能在其他 Window 函数中嵌套 Window 函数。

* 不能在聚合函数内部使用 Window。

* Window 需要能够扫描整个表格。

* WRank()、RecNo() 和 RowNo() 在使用滑动窗口功能时不能与 Window一起使用。