I'm trying to get a table of IDs if their transaction values are between the 0.10 and 0.25 percentiles, as follows:
select case when transaction_total_value >= percentile_cont(0.10) WITHIN GROUP (order by transaction_total_value)
and transaction_total_value <= percentile_cont(0.25) WITHIN GROUP (order by transaction_total_value)
then id_customer
else null
end as id_customer
into tmp_value_4_1
from tmp_value_2_1
group by transaction_total_value,id_customer;
But doing this way it returns the entire series of customers ID, not only between the percentiles range.
What I'm doing wrong in this case?
Thanks!
CodePudding user response:
with cte as (
select
id_customer,
transaction_total_value,
percentile_cont(0.10) within group (order by transaction_total_value asc) as percentile_10,
percentile_cont(0.25) within group (order by transaction_total_value asc) as percentile_25
from tmp_value_2_1
)
select id_customer
from cte
where transaction_total_value betweeen
percentile_10 and percentile_25
