In the process of analysis, we often need to calculate the quantile of a feature. Here is how to calculate the quantile of a certain column of data in Oracle.
The structure of the table that requires quantiles is as follows:
select * from test_lizhen;
We found that the table has two columns, one for different products, and one for the attributes of each user. We can calculate the quantile of the feature by the following method
1) Regardless of product, calculate the quantile of all users
select PERCENTILE_CONT(0) within group(order by pltf_cnt_60m) as max_sal_0,
PERCENTILE_CONT(0.2) within group(order by pltf_cnt_60m) as max_sal_20,
PERCENTILE_CONT(0.4) within group(order by pltf_cnt_60m) as max_sal_40,
PERCENTILE_CONT(0.6) within group(order by pltf_cnt_60m) as max_sal_60,
PERCENTILE_CONT(0.8) within group(order by pltf_cnt_60m) as max_sal_80,
PERCENTILE_CONT(1) within group(order by pltf_cnt_60m) as max_sal_100
from test_lizhen;
The results are as follows:
2) Differentiate products and calculate the quantile of users of different products
select distinct product_no,
PERCENTILE_CONT(0) within group(order by pltf_cnt_60m) over(partition by product_no) max_sal_0,
PERCENTILE_CONT(0.2) within group(order by pltf_cnt_60m) over(partition by product_no) max_sal_0,
PERCENTILE_CONT(0.4) within group(order by pltf_cnt_60m) over(partition by product_no) max_sal_0,
PERCENTILE_CONT(0.6) within group(order by pltf_cnt_60m) over(partition by product_no) max_sal_0,
PERCENTILE_CONT(0.8) within group(order by pltf_cnt_60m) over(partition by product_no) max_sal_0,
PERCENTILE_CONT(1) within group(order by pltf_cnt_60m) over(partition by product_no) max_sal_0
from test_lizhen;
The results are as follows:
We can also calculate the overall position of the variable for a certain user:
PERCENT_RANK() over(partition by product_no order by pltf_cnt_60m) p_rank