# autonumber 脚本函数

此脚本函数用于返回在脚本执行期间 expression 遇到的每个特殊计算值的整数值。此函数可用于创建复合键的紧凑记忆呈现形式。

信息注释您只能连接已在同一数据加载过程中生成的 autonumber 关键字段，作为根据读取表格的顺序所生成的整数。如果您需要使用在两次数据加载过程中保持一致且独立于源数据排序的关键字段，应使用 hash128、hash160 或 hash256 函数。

语法：

autonumber(expression[ , AutoID])

参数：

| 参数 | 描述 |
| --- | --- |
| AutoID | 为创建多个计数实例，如果 autonumber 函数用于脚本内不同的字段，则可选参数 AutoID 将用于命名每个计数。 |

示例： 创建合成钥段

在此例中，我们使用 autonumber 函数创建合成钥段以节省内存。尽管此例主要用于演示目的，但对于包含大量行的表格将非常有意义。

示例数据

| Region | Year | Month | Sales |
| --- | --- | --- | --- |
| North | 2014 | May | 245 |
| North | 2014 | May | 347 |
| North | 2014 | June | 127 |
| South | 2014 | June | 645 |
| South | 2013 | May | 367 |
| South | 2013 | May | 221 |

使用内联数据加载源数据。然后，我们添加前置 Load，用来从 Region、Year 和 Month 字段创建合成钥段。

```
RegionSales:
LOAD *,
AutoNumber(Region&Year&Month) as RYMkey;

LOAD * INLINE
[ Region, Year, Month, Sales
North,	2014,	May,	245
North,	2014,	May,	347
North,	2014,	June,	127
South,	2014,	June,	645
South,	2013,	May, 367
South,	2013,	May,	221
];
```

最终生成的表格如下所示：

结果表

| Region | Year | Month | Sales | RYMkey |
| --- | --- | --- | --- | --- |
| North | 2014 | May | 245 | 1 |
| North | 2014 | May | 347 | 1 |
| North | 2014 | June | 127 | 2 |
| South | 2014 | June | 645 | 3 |
| South | 2013 | May | 367 | 4 |
| South | 2013 | May | 221 | 4 |

在此例中，如果您需要链接到其他表格，可以引用 RYMkey（例如 1），而不是字符串“North2014May”。

现在，我们可以通过类似方式加载成本的源表格。在前置 Load 中已排除 Region、Year 和 Month 字段以避免创建合成钥，因为我们已经使用 autonumber 函数创建复合关键字段用于链接表格。

```
RegionCosts:
LOAD Costs,
AutoNumber(Region&Year&Month) as RYMkey;

LOAD * INLINE
[ Region, Year, Month, Costs
South,	2013,	May,	167
North,	2014,	May,	56
North,	2014,	June,	199
South,	2014,	June,	64
South,	2013,	May, 172
South,	2013,	May,	126
];
```

现在，我们可以将表格可视化添加到工作表，并添加 Region、Year 和 Month 字段，以及销售额和成本的 Sum 度量。表格将如下所示：

结果表

| Region | Year | Month | Sum([Sales]) | Sum([Costs]) |
| --- | --- | --- | --- | --- |
| Totals | - | - | 1952 | 784 |
| North | 2014 | June | 127 | 199 |
| North | 2014 | May | 592 | 56 |
| South | 2014 | June | 645 | 64 |
| South | 2013 | May | 588 | 465 |

## 了解详情

* [autonumberhash128 脚本函数](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Sense_Hub/Scripting/CounterFunctions/autonumberhash128.htm)
* [autonumberhash256 脚本函数](https://help.qlik.com/zh-CN/cloud-services/Subsystems/Hub/Content/Sense_Hub/Scripting/CounterFunctions/autonumberhash256.htm)