SQL FIRST_VALUE


Example

You use the FIRST_VALUE function to determine the first value in an ordered result set, which you identify using a scalar expression.

SELECT StateProvinceID, Name, TaxRate,
       FIRST_VALUE(StateProvinceID)
        OVER(ORDER BY TaxRate ASC) AS FirstValue
FROM SalesTaxRate;

In this example, the FIRST_VALUE function is used to return the ID of the state or province with the lowest tax rate. The OVER clause is used to order the tax rates to obtain the lowest rate.

StateProvinceIDNameTaxRateFirstValue
74Utah State Sales Tax5.0074
36Minnesota State Sales Tax6.7574
30Massachusetts State Sales Tax7.0074
1Canadian GST7.0074
57Canadian GST7.0074
63Canadian GST7.0074