LAST_VALUE (Transact-SQL)LAST_VALUE (Transact-SQL)
適用対象:Applies to: SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
SQL ServerSQL Server (サポートされているすべてのバージョン)
SQL ServerSQL Server (all supported versions)
Azure SQL データベースAzure SQL Database
Azure SQL データベースAzure SQL Database
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure SQL Managed InstanceAzure SQL Managed Instance
Azure Synapse AnalyticsAzure Synapse Analytics
Azure Synapse AnalyticsAzure Synapse Analytics
順序付けられた値のセットで最後の値を返します。Returns the last value in an ordered set of values.
Transact-SQL 構文表記規則
Transact-SQL Syntax Conventions
構文Syntax
LAST_VALUE ( [ scalar_expression ] ) [ IGNORE NULLS | RESPECT NULLS ]
OVER ( [ partition_by_clause ] order_by_clause rows_range_clause )
注意
SQL Server 2014 以前の Transact-SQL 構文を確認するには、以前のバージョンのドキュメントを参照してください。To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
引数Arguments
scalar_expressionscalar_expression
返される値。Is the value to be returned. scalar_expression 列、サブクエリ、または 1 つの値となるその他の式を指定できます。scalar_expression can be a column, subquery, or other expression that results in a single value. 他の分析関数は指定できません。Other analytic functions are not permitted.
[ IGNORE NULLS | RESPECT NULLS ][ IGNORE NULLS | RESPECT NULLS ]
適用対象:Azure SQL EdgeApplies to: Azure SQL Edge
IGNORE NULLS: パーティションの最後の値の計算時に、データセット内の null 値を無視します。IGNORE NULLS - Ignore null values in the dataset when computing the last value over a partition.
RESPECT NULLS: パーティションの最後の値の計算時に、データセット内の null 値を使用します。RESPECT NULLS - Respect null values in the dataset when computing last value over a partition.
詳細については、欠損値の補完に関する記事を参照してください。For more information, see Imputing missing values.
OVER ( [ partition_by_clause ] order_by_clause [ rows_range_clause ] )OVER ( [ partition_by_clause ] order_by_clause [ rows_range_clause ] )
partition_by_clause は、FROM 句で生成された結果セットをパーティションに分割します。このパーティションに関数が適用されます。partition_by_clause divides the result set produced by the FROM clause into partitions to which the function is applied. 指定しない場合、関数ではクエリ結果セットのすべての行を 1 つのグループとして扱います。If not specified, the function treats all rows of the query result set as a single group.
order_by_clause は、関数を適用する前にデータの順序を決定します。order_by_clause determines the order of the data before the function is applied. order_by_clause が必要です。The order_by_clause is required. rows_range_clause は始点と終点を指定することによって、パーティション内の行をさらに制限します。rows_range_clause further limits the rows within the partition by specifying start and end points. 詳細については、を参照してください。 OVER 句 (Transact-SQL).For more information, see OVER Clause (Transact-SQL).
戻り値の型Return Types
同じ型には scalar_expression です。Is the same type as scalar_expression.
全般的な解説General Remarks
LAST_VALUE は非決定的です。LAST_VALUE is nondeterministic. 詳細については、「 決定的関数と非決定的関数」を参照してください。For more information, see Deterministic and Nondeterministic Functions.
例Examples
A.A. パーティションで LAST_VALUE を使用するUsing LAST_VALUE over partitions
次の例では、特定の給与 (等級) について各部門の最後の従業員の採用日を返します。 The following example returns the hire date of the last employee in each department for the given salary (Rate). PARTITION BY 句によって部門ごとに従業員がパーティションに分割され、各パーティションに個別に LAST_VALUE 関数が適用されます。The PARTITION BY clause partitions the employees by department and the LAST_VALUE function is applied to each partition independently. OVER 句に指定された ORDER BY 句は、各パーティション内の行に LAST_VALUE 関数が適用される論理的な順序を決定します。The ORDER BY clause specified in the OVER clause determines the logical order in which the LAST_VALUE function is applied to the rows in each partition.
USE AdventureWorks2012;
GO
SELECT Department, LastName, Rate, HireDate,
LAST_VALUE(HireDate) OVER (PARTITION BY Department ORDER BY Rate) AS LastValue
FROM HumanResources.vEmployeeDepartmentHistory AS edh
INNER JOIN HumanResources.EmployeePayHistory AS eph
ON eph.BusinessEntityID = edh.BusinessEntityID
INNER JOIN HumanResources.Employee AS e
ON e.BusinessEntityID = edh.BusinessEntityID
WHERE Department IN (N'Information Services',N'Document Control');
結果セットは次のようになります。Here is the result set.
Department LastName Rate HireDate LastValue
--------------------------- ----------------------- ------------ ---------- ----------
Document Control Chai 10.25 2003-02-23 2003-03-13
Document Control Berge 10.25 2003-03-13 2003-03-13
Document Control Norred 16.8269 2003-04-07 2003-01-17
Document Control Kharatishvili 16.8269 2003-01-17 2003-01-17
Document Control Arifin 17.7885 2003-02-05 2003-02-05
Information Services Berg 27.4038 2003-03-20 2003-01-24
Information Services Meyyappan 27.4038 2003-03-07 2003-01-24
Information Services Bacon 27.4038 2003-02-12 2003-01-24
Information Services Bueno 27.4038 2003-01-24 2003-01-24
Information Services Sharma 32.4519 2003-01-05 2003-03-27
Information Services Connelly 32.4519 2003-03-27 2003-03-27
Information Services Ajenstat 38.4615 2003-02-18 2003-02-23
Information Services Wilson 38.4615 2003-02-23 2003-02-23
Information Services Conroy 39.6635 2003-03-08 2003-03-08
Information Services Trenary 50.4808 2003-01-12 2003-01-12
B.B. 計算された式で FIRST_VALUE と LAST_VALUE を使用するUsing FIRST_VALUE and LAST_VALUE in a computed expression
次の例では、計算された式で FIRST_VALUE 関数と LAST_VALUE 関数を使用して、指定された人数の従業員について、その年の現在の四半期の販売ノルマの値と第 1 四半期および第 4 四半期の販売ノルマの値の差をそれぞれ示します。The following example uses the FIRST_VALUE and LAST_VALUE functions in computed expressions to show the difference between the sales quota value for the current quarter and the first and last quarter of the year respectively for a given number of employees. FIRST_VALUE 関数は、その年の第 1 四半期の販売ノルマの値を返し、その値を現在の四半期の販売ノルマの値から引きます。The FIRST_VALUE function returns the sales quota value for the first quarter of the year, and subtracts it from the sales quota value for the current quarter. そして、その結果を DifferenceFromFirstQuarter という派生列に返します。It is returned in the derived column entitled DifferenceFromFirstQuarter. その年の第 1 四半期については、DifferenceFromFirstQuarter 列の値は 0 です。For the first quarter of a year, the value of the DifferenceFromFirstQuarter column is 0. LAST_VALUE 関数は、その年の最終四半期の販売ノルマの値を返し、その値を現在の四半期の販売ノルマの値から引きます。The LAST_VALUE function returns the sales quota value for the last quarter of the year, and subtracts it from the sales quota value for the current quarter. そして、その結果を DifferenceFromLastQuarter という派生列に返します。 It is returned in the derived column entitled DifferenceFromLastQuarter. その年の最終四半期については、DifferenceFromLastQuarter 列の値は 0 です。For the last quarter of a year, the value of the DifferenceFromLastQuarter column is 0.
以下に示すように、この例の場合、DifferenceFromLastQuarter 列でゼロ以外の値を返すには、"RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" 句が必要です。The clause "RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING" is required in this example for the non-zero values to be returned in the DifferenceFromLastQuarter column, as shown below. 既定の範囲は "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW" です。The default range is "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW". この例では、この既定の範囲を使用した場合 (または、範囲を含めない結果、既定値が使用される場合)、DifferenceFromLastQuarter 列にゼロが返されます。In this example, using that default range (or not including a range, resulting in the default being used) would result in zeroes being returned in the DifferenceFromLastQuarter column. 詳細については、を参照してください。 OVER 句 (Transact-SQL).For more information, see OVER Clause (Transact-SQL).
USE AdventureWorks2012;
SELECT BusinessEntityID, DATEPART(QUARTER,QuotaDate)AS Quarter, YEAR(QuotaDate) AS SalesYear,
SalesQuota AS QuotaThisQuarter,
SalesQuota - FIRST_VALUE(SalesQuota)
OVER (PARTITION BY BusinessEntityID, YEAR(QuotaDate)
ORDER BY DATEPART(QUARTER,QuotaDate) ) AS DifferenceFromFirstQuarter,
SalesQuota - LAST_VALUE(SalesQuota)
OVER (PARTITION BY BusinessEntityID, YEAR(QuotaDate)
ORDER BY DATEPART(QUARTER,QuotaDate)
RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING ) AS DifferenceFromLastQuarter
FROM Sales.SalesPersonQuotaHistory
WHERE YEAR(QuotaDate) > 2005
AND BusinessEntityID BETWEEN 274 AND 275
ORDER BY BusinessEntityID, SalesYear, Quarter;
結果セットは次のようになります。Here is the result set.
BusinessEntityID Quarter SalesYear QuotaThisQuarter DifferenceFromFirstQuarter DifferenceFromLastQuarter
---------------- ----------- ----------- --------------------- --------------------------- -----------------------
274 1 2006 91000.00 0.00 -63000.00
274 2 2006 140000.00 49000.00 -14000.00
274 3 2006 70000.00 -21000.00 -84000.00
274 4 2006 154000.00 63000.00 0.00
274 1 2007 107000.00 0.00 -9000.00
274 2 2007 58000.00 -49000.00 -58000.00
274 3 2007 263000.00 156000.00 147000.00
274 4 2007 116000.00 9000.00 0.00
274 1 2008 84000.00 0.00 -103000.00
274 2 2008 187000.00 103000.00 0.00
275 1 2006 502000.00 0.00 -822000.00
275 2 2006 550000.00 48000.00 -774000.00
275 3 2006 1429000.00 927000.00 105000.00
275 4 2006 1324000.00 822000.00 0.00
275 1 2007 729000.00 0.00 -489000.00
275 2 2007 1194000.00 465000.00 -24000.00
275 3 2007 1575000.00 846000.00 357000.00
275 4 2007 1218000.00 489000.00 0.00
275 1 2008 849000.00 0.00 -20000.00
275 2 2008 869000.00 20000.00 0.00
(20 row(s) affected)