# FirstSortedValue

FirstSortedValue() 返回在 value 中指定的表达式的值，该值与 sort\_weight 参数的排序结果对应，如果指定，还要考虑 rank。如果在指定 rank 下多个结果值共享同一 sort\_weight，则此函数返回 NULL。

语法：

FirstSortedValue([{SetExpression}] [DISTINCT] [TOTAL [&lt;fld {,fld}&gt;]] value, sort\_weight [,rank])

返回数据类型： 双

参数：

* value: 输出字段。 此函数用于查找表达式 value 的值，相当于 sort\_weight 的排序结果。

* sort\_weight: 输入字段。 该表达式包含要排序的数据。找到 sort\_weight 的第一个（最低）值，由 value 表达式的对应值确定。如果在 sort\_weight 前面加一个减号，则此函数会返回最后一个（最高）排序值。

* rank: 通过指定一个大于 1 的 rank “n”，您会获得第 n 个排序值。

* SetExpression: 聚合函数会默认聚合选择项定义的可能记录集合。可选记录集合可由集合分析表达式定义。

* DISTINCT: 如果在函数参数前出现单词 DISTINCT，则将忽略计算该函数参数生成的副本。

* TOTAL: 如果在函数参数前面出现单词 TOTAL，则计算给出当前选择项的所有可能值，而不只是属于当前维度值的那些值，即它会忽略图表维度。 TOTAL 限定符后可能紧跟着一系列由尖括号括起来的一个或多个字段名 &lt;fld&gt;。这些字段名应该是图表维度变量的子集。

  [定义聚合范围](https://help.qlik.com/zh-CN/qlikview/September2026/Subsystems/Client/Content/QV_QlikView/ChartFunctions/define-aggregation-scope.htm)

示例：

示例和结果

| 示例 | 结果 |
| --- | --- |
| `firstsortedvalue (Product, UnitPrice)` | BB，这是具有最低 `Product`(9) 的 `UnitPrice`。 |
| `firstsortedvalue (Product, UnitPrice, 2)` | BB，这是具有第二低 `UnitPrice`(10) 的 `Product`。 |
| `firstsortedvalue (Customer, -UnitPrice, 2)` | Betacab，这是具有第二高 `Customer`(20) 的 `Product` 的 `UnitPrice`。 |
| `firstsortedvalue (Customer, UnitPrice, 3)` | NULL，因为有两个相同 `Customer`（第三低）Astrida(15) 的 Canutility 值（rank 和 `UnitPrice`）。<br><br>使用 distinct 限定符可确保不会产生意外 NULL 结果。 |
| `firstsortedvalue (Customer, -UnitPrice*UnitSales, 2)` | Canutility，这是具有第二高销售订单值（`Customer` 乘以 `UnitPrice` (120)）的 `UnitSales`。 |

**示例中所使用的数据：**

`ProductData:` `LOAD * inline [` `Customer|Product|UnitSales|UnitPrice` `Astrida|AA|4|16` `Astrida|AA|10|15` `Astrida|BB|9|9` `Betacab|BB|5|10` `Betacab|CC|2|20` `Betacab|DD||25` `Canutility|AA|8|15` `Canutility|CC||19` `] (delimiter is '|');`

表格形式的示例数据

| Customer | Product | UnitSales | UnitPrice |
| --- | --- | --- | --- |
| Astrida | AA | 4 | 16 |
| Astrida | AA | 10 | 15 |
| Astrida | BB | 9 | 9 |
| Betacab | BB | 5 | 10 |
| Betacab | CC | 2 | 20 |
| Betacab | DD | - | 25 |
| Canutility | AA | 8 | 15 |
| Canutility | CC | - | 19 |

## 了解详情

* [集合分析和集合表达式](https://help.qlik.com/zh-CN/qlikview/September2026/Subsystems/Client/Content/QV_QlikView/ChartFunctions/SetAnalysis/set-analysis-expressions.htm)
* [定义聚合范围](https://help.qlik.com/zh-CN/qlikview/September2026/Subsystems/Client/Content/QV_QlikView/ChartFunctions/define-aggregation-scope.htm)