SQL Server Analytical Functions
SQL Server Analytical Functions
MAX
BINARY_CHECKSUM MIN
CHECKSUM
SUM
CHECKSUM_AGG
STDEV
COUNT
STDEVP
COUNT_BIG
VAR
GROUPING
VARP
job
sal
hiredate
hiredate
unbounded following
) job_avg
,
hiredate
between current row
and 2 following
) job_avg
from emp
where sal < 2500
order
by
job
FIRST_VALUE: Returns the first value in an ordered set of values in SQL Server 2012.
Syntax:1) FIRST_VALUE (column) over (partition by column order by column)
2) FIRST_VALUE (column) ignore nulls over (partition by column order by column)
Notes
When you ORDER a set of records in analytic functions you can specify a range of rows to consider,
ignoring the others. You can do this using the ROWS clause
UNBOUNDED PRECEDING the range starts at the first row of the partition.
UNBOUNDED FOLLOWING The range ends at the last row of the partition.
CURRENT ROW range begins at the current row or ends at the current row
n PRECEDING or n FOLLOWING The range starts or ends n rows before or after the current row
LEAD and LAG: The LAG/LEAD functions return a column from a previous/following row in
the partition, with respect to the current row, as specified by the row offset in the function,
without the use of a self-join.
SELECT s.SalesOrderID,s.SalesOrderDetailID,s.OrderQty,
LEAD(SalesOrderDetailID) OVER (ORDER BY SalesOrderDetailID
) LeadValue,
LAG(SalesOrderDetailID) OVER (ORDER BY SalesOrderDetailID
) LagValue
FROM Sales.SalesOrderDetail s
WHERE SalesOrderID IN (43670, 43669, 43667, 43663)
ORDER BY s.SalesOrderID,s.SalesOrderDetailID,s.OrderQty
PERCENT_RANK():This function returns relative standing of a value within a query result set
or partition.
The formula to find PERCENT_RANK () is as following:
LAST
LAST_VALUE
LEAD
MAX
MIN
NTILE
PERCENT_RANK
PERCENTILE_CONT
PERCENTILE_DISC
RANK
RATIO_TO_REPORT
REGR_ (Linear Regression) Functions
ROW_NUMBER
STDDEV
STDDEV_POP
STDDEV_SAMP
SUM
VAR_POP
VAR_SAMP
VARIANCE
New analytical functions in 11g
NTH_VALUE
ListAgg
Details
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions004.htm
Reference Links
http://msdn.microsoft.com/en-us/library/hh213234.aspx
http://technology.amis.nl/2004/10/04/analytical-sql-functions-theory-and-examples-part-2-on-theorder-by-and-windowing-clauses/2/
http://blog.sqlauthority.com/2007/10/09/sql-server-2005-sample-example-of-ranking-functionsrow_number-rank-dense_rank-ntile/