Window Functions
Recursive Queries
RFM Framework, to see the unified view of customer with his profile and all the products usage
Recursive - to meet the operational needs
Window Functions, : These are powerful tools for performing calculations across set of table -context of the current row, unlike ogher aggregate/groupby windows functions do not cause rows to be grouped into single output row, mean you can compare/compute current row with various other measure required for analytical/operational purposes.
<function_name>(<expression>) OVER ( [PARTITION BY <expression_list>] [ORDER BY <expression_list>] [<frame_clause>])Various Functions
ROW_NUMBER(): Assigns a unique sequential integer to rows within a partition.
RANK(): Assigns a rank to each row within a partition, with gaps in ranking values if there are ties.
DENSE_RANK(): Similar to RANK(), but without gaps in ranking values.
NTILE(n): Divides the rows in each partition into n buckets and assigns a bucket number to each row.
SUM(expression): Calculates the sum of the specified expression.
AVG(expression): Calculates the average value of the specified expression.
MIN(expression): Finds the minimum value of the specified expression.
MAX(expression): Finds the maximum value of the specified expression.
COUNT(expression): Counts the number of rows or non-null values of the specified expression.
FIRST_VALUE(expression): Returns the first value in the specified expression.
LAST_VALUE(expression): Returns the last value in the specified expression.
NTH_VALUE(expression, n): Returns the n-th value in the specified expression.
LEAD(expression, offset, default_value): Returns the value of the specified expression from a subsequent row.
LAG(expression, offset, default_value): Returns the value of the specified expression from a preceding row.
CUME_DIST(): Computes the cumulative distribution of a value in a group of values.
PERCENT_RANK(): Computes the relative rank of a value in a group of values.
MATCH_NUMBER(): Returns the match number of the row in the pattern.
CLASSIFIER(): Returns the name of the primary pattern variable to which the row is mapped.
PERCENTILE_CONT(percentile) WITHIN GROUP (ORDER BY expression): Computes the continuous percentile.
PERCENTILE_DISC(percentile) WITHIN GROUP (ORDER BY expression): Computes the discrete percentile.
Practical use cases.
SELECT SalesID,
SaleDate,
Amount,
SUM(Amount) OVER (ORDER BY SaleDate) AS CumulativeSales
FROM Sales;
======================