Percentile_cont is a statistical function in BigQuery that calculates the specified percentile of a continuous variable. Here's how you can use it in BigQuery:
Syntax:
code
PERCENTILE_CONT(expression, percentile)
expression: The expression to be calculated. This can be a scalar or a table.
percentile: The percentile to be calculated, e.g. 50th percentile.
Example:
code
SELECT PERCENTILE_CONT(AVG(age), 50) FROM customers;
This will calculate the 50th percentile of the average age of the customers in the table.
Parameters:
expression: The expression to be calculated. This can be a scalar or a table.
percentile: The percentile to be calculated. This can be a string or an integer, representing the percentile to be calculated.
Example:
code
SELECT PERCENTILE_CONT(SUM(amount), 75) FROM transactions;
This will calculate the 75th percentile of the sum of the amounts in the transactions table.
Options:
agg_statistics: (optional) This option allows you to specify the aggregation statistic to be used for the calculation. For example, agg_statistics = 'mean' will calculate the mean of the values rather than the sum.
column_statistics: (optional) This option allows you to specify the column statistics to be used for the calculation. For example, column_statistics = 'skewness' will calculate the skewness of the values rather than the mean.
Example:
code
SELECT PERCENTILE_CONT(SUM(amount), 75, agg_statistics = 'variance') FROM transactions;
This will calculate the 75th percentile of the sum of the amounts in the transactions table, using the variance of the values rather than the mean.
Data types:
expression: The data type of the expression to be calculated is inferred based on the data type of the table being queried. For example, if the table contains integers, the expression will be inferred as an integer.
percentile: The data type of the percentile is inferred based on the data type of the expression to be calculated. For example, if the expression is an integer, the percentile will be inferred as an integer.
Examples:
Calculate the 75th percentile of the average age of customers in a table:
code
SELECT PERCENTILE_CONT(AVG(age), 75) FROM customers;
Calculate the 95th percentile of the sum of the amounts in a table:
code
SELECT PERCENTILE_CONT(SUM(amount), 95) FROM transactions;
Calculate the 80th percentile of the variance of the amounts in a table:
code
SELECT PERCENTILE_CONT(VAR(amount), 80) FROM transactions;
Note: Percentile_cont is a powerful function that can be used to calculate percentiles of a continuous variable. However, it's important to be careful when interpreting the results, as the percentile can be sensitive to the choice of aggregation and column statistics.