Getting the right behaviour of Excel's PERCENTRANK.EXC on mySQL

34 Views Asked by At

I would like to replicate Excel's PERECENTRANK.EXC() functionality and behaviour on SQL.

Sample Data set:

enter image description here

From what I understand from PERCENTRANK.EXC(), it excludes the first and last value of the array.

However in SQL, I can't seem to find a way to replicate this, as PERCENT_RANK() in SQL seem to always give a value of 0 and 1 to the minimum and maximum values, thus deviating from what PERCENTRANK.EXC() in Excel does:

enter image description here

In SQL, this is how I tried to script it:

SELECT 
    market,
    campaign_coount,
    revenue,
    PERCENT_RANK() OVER (PARTITION BY market ORDER BY revenue) as percentile
FROM data_table
GROUP BY 1,2,3
ORDER BY 1 ASC, 3 DESC

Is there some variable input or some other RANK methods in SQL that I can use to mimic Excel's PERCENTRANK.EXC() function completely?

Edit: Ignoring trying to replicate the Excel's function; how do I write the query in SQL with PERCENT_RANK() such that it takes the value, but the array is excluding the first and last value when grouped by Market?

Thanks!

0

There are 0 best solutions below