# FirstSortedValue

FirstSortedValue() 會傳回在 value (對應於 sort\_weight 引數的排序結果) 中指定的運算式的值，並考慮到 rank (若已指定)。如果多個結果值對指定的 rank 共用同一個 sort\_weight，函數會傳回 NULL。

語法：

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

傳回的資料類型： 雙值

引數：

* value：輸出欄位。 該函數會找出對應於 sort\_weight 排序結果的 value 運算式的值。

* sort\_weight：輸入欄位。 包含待排序資料的運算式。會找出 sort\_weight 的第一個 (最低) 值，可從中決定 value 運算式的對應值。如果在 sort\_weight 前面加上減號，則函數會傳回最後一個 (最高) 排序值。

* rank： 表示 rank "n" 大於 1 之後，將取得第 n 個排序的值。

* SetExpression: 依照預設，彙總函數將彙總選項所定義的一組可能記錄。集合分析運算式可定義一組替代的記錄。

* DISTINCT: 如果 DISTINCT 這個字出現在函數引數之前，會忽略評估函數引數所產生的重複項目。

* TOTAL： 如果 TOTAL 這個字出現在函數引數之前，會計算已指定目前選項的所有可能值，而不僅是與目前維度值相關的值，也就是說，會忽略圖表維度。 TOTAL 限定詞後面可以加上以角括弧 &lt;fld&gt; 括住的一或多個欄位名稱。這些欄位名稱應該是圖表維度變數的子集。

  [定義彙總範圍](https://help.qlik.com/zh-TW/qlikview/September2026/Subsystems/Client/Content/QV_QlikView/ChartFunctions/define-aggregation-scope.htm)

範例：

範例與結果

| 範例 | 結果 |
| --- | --- |
| `firstsortedvalue (Product, UnitPrice)` | BB，即為 `UnitPrice` 最低的 `Product`(9)。 |
| `firstsortedvalue (Product, UnitPrice, 2)` | BB，即為 `UnitPrice` 第二低的 `Product`(10)。 |
| `firstsortedvalue (Customer, -UnitPrice, 2)` | Betacab，即為擁有第二高 `UnitPrice` 的 `Product` 之 `Customer`(20)。 |
| `firstsortedvalue (Customer, UnitPrice, 3)` | NULL，因為有兩個 `Customer` 值 (Astrida 和 Canutility) 具有相同 rank (第三低) `UnitPrice`(15)。<br><br>使用 distinct 限定詞以確定不會發生非預期的 NULL 結果。 |
| `firstsortedvalue (Customer, -UnitPrice*UnitSales, 2)` | Canutility，即為擁有第二高銷售訂單值 `UnitPrice` 乘以 `UnitSales` 的 `Customer`(120)。 |

**範例中使用的資料：**

`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-TW/qlikview/September2026/Subsystems/Client/Content/QV_QlikView/ChartFunctions/SetAnalysis/set-analysis-expressions.htm)
* [定義彙總範圍](https://help.qlik.com/zh-TW/qlikview/September2026/Subsystems/Client/Content/QV_QlikView/ChartFunctions/define-aggregation-scope.htm)