PERCENTILE_CONT and PERCENTILE_DISC functions #2#8946
PERCENTILE_CONT and PERCENTILE_DISC functions #2#8946dyemanov merged 2 commits intoFirebirdSQL:masterfrom
Conversation
@sim1984 What exactly do you mean with this? Because the expression is not constant, as it is used to determine which value to output and varies with each row. |
That's exactly it. If the PERCENTILE_CONT/PERCENTILE_DISC argument Full example #7632 (comment) |
|
Ah, right. Sorry, I was busy in my head with the expression in the WITHIN GROUP, and got them mixed up when re-reading that part. |
+ also add them to fblangref60
PERCENTILE_DISC and PERCENTILE_CONT functions
The
PERCENTILE_CONTandPERCENTILE_DISCfunctions are known as inverse distribution functions.These functions operate on an ordered set. Both functions can be used as aggregate or window functions.
PERCENTILE_DISC
PERCENTILE_DISCis an inverse distribution function that assumes a discrete distribution model.It takes a percentile value and a sort specification and returns an element from the set.
Nulls are ignored in the calculation.
Syntax for the
PERCENTILE_DISCfunction as an aggregate function.Syntax for the
PERCENTILE_DISCfunction as an window function.The first argument
<percent>must evaluate to a numeric value between 0 and 1, because it is a percentile value.This expression must be constant within each aggregate group.
The
ORDER BYclause takes a single expression that can be of any type that can be sorted.The function
PERCENTILE_DISCreturns a value of the same type as the argument inORDER BY.For a given percentile value
P,PERCENTILE_DISCsorts the values of the expression in theORDER BYclause andreturns the value with the smallest
CUME_DISTvalue (with respect to the same sort specification)that is greater than or equal to
P.Analytic Example
PERCENTILE_CONT
PERCENTILE_CONTis an inverse distribution function that assumes a continuous distribution model.It takes a percentile value and a sort specification and returns an element from the set.
Nulls are ignored in the calculation.
Syntax for the
PERCENTILE_CONTfunction as an aggregate function.Syntax for the
PERCENTILE_CONTfunction as an window function.The first argument
<percent>must evaluate to a numeric value between 0 and 1, because it is a percentile value.This expression must be constant within each aggregate group.
The
ORDER BYclause takes a single expression, which must be of numeric type to perform interpolation.The
PERCENTILE_CONTfunction returns a value of typeDOUBLE PRECISIONorDECFLOAT(34)depending on the typeof the argument in the
ORDER BYclause. A value of typeDECFLOAT(34)is returned ifORDER BYcontainsan expression of one of the types
INT128,NUMERIC(38, x)orDECFLOAT(16 | 34), otherwise -DOUBLE PRECISION.The result of
PERCENTILE_CONTis computed by linear interpolation between values after ordering them.Using the percentile value (
P) and the number of rows (N) in the aggregation group, you can computethe row number you are interested in after ordering the rows with respect to the sort specification.
This row number (
RN) is computed according to the formulaRN = (1 + (P * (N - 1)).The final result of the aggregate function is computed by linear interpolation between the values from rows
at row numbers
CRN = CEILING(RN)andFRN = FLOOR(RN).Analytic Example
An example of using both aggregate functions
This PR is a copy of #8807 but with cleaner changes.