I previously posted this related question. The solution to this question works for the analytic function rank(), however it does not work for percent_rank() in the correct manner. To demonstrate, I have the following dummy table:
with
table as (
select 'a' as category, 1 as num, 15 as num2, 7 as cutoff union all
select 'a' as category, 2 as num, 15 as num2, 7 as cutoff union all
select 'a' as category, 3 as num, 5 as num2, 7 as cutoff union all
select 'a' as category, 4 as num, 5 as num2, 7 as cutoff union all
select 'a' as category, 5 as num, 5 as num2, 7 as cutoff union all
select 'a' as category, 6 as num, 5 as num2, 7 as cutoff union all
select 'a' as category, 7 as num, 5 as num2, 7 as cutoff union all
select 'a' as category, 8 as num, 5 as num2, 7 as cutoff union all
select 'a' as category, 9 as num, 5 as num2, 7 as cutoff union all
select 'a' as category, 10 as num, 15 as num2, 7 as cutoff union all
select 'a' as category, 11 as num, 15 as num2, 7 as cutoff union all
select 'a' as category, 12 as num, 15 as num2, 7 as cutoff union all
select 'a' as category, 13 as num, 15 as num2, 7 as cutoff union all
select 'a' as category, 14 as num, 15 as num2, 7 as cutoff union all
select 'a' as category, 15 as num, 15 as num2, 7 as cutoff union all
select 'a' as category, 16 as num, 15 as num2, 7 as cutoff union all
select 'a' as category, 17 as num, 5 as num2, 7 as cutoff union all
select 'a' as category, 18 as num, 15 as num2, 7 as cutoff union all
select 'a' as category, 19 as num, 15 as num2, 7 as cutoff union all
select 'a' as category, 20 as num, 5 as num2, 7 as cutoff union all
select 'a' as category, 21 as num, 5 as num2, 7 as cutoff
)
percent_rank()'s are needed for the num column. However, only rows where num2 > cutoff should be considered for the percentile rankings. I have attempted the following 2 ways to compute percentiles, with the output:
select
*,
if(num2 >= cutoff,
percent_rank() over(
partition by category
order by num
), null) as pctile1,
if(num2 >= cutoff,
percent_rank() over(
partition by category
order by if (num2 >= cutoff, num, null) ASC
), null) as pctile2
from table
order by num asc
Both pctile1 and pctile2 are incorrect. To show why this is the case, look at row 10, which has pctile1 == 0.45 and pctile2 == 0.60. However, amongst qualifying values, this should be a lower percentile. There are only 2 qualifying values lower than num == 10 (that being 1 and 2), whereas there are many values above 10 that qualify (11 - 19). The correct percentile for num == 10, given the num2 and cutoff values, should be closer to 30%, since 10 is the 3rd lowest value amongst the 11 qualifying values.
Note that I should not filter table to remove the rows I am not percent_rank()'ing over, as I need to keep these rows.
Edit
I am not sure how to shrink image size but I am trying to do that currently.
