I would like the PERCENT_RANK
value for a single record, but in relation to the entire table. Is this possible?
Examples I've seen are like this:
SELECT Name, Salary
PERCENT_RANK() OVER (ORDER BY Salary)
FROM Employees
Notice that it's calculating the percentile for the returned recordset. If you're returning the entire table, that's great. But if you're returning a subset, then you're only going to get the percentile for a row within the returned recordset, when I want it for the entire table (for a group of rows I don't return).
The doc says this:
Calculates the relative rank of a row within a group of rows
This is not what I want. Basically, I want to do this (note: this is non-working code):
SELECT Name, Salary
PERCENT_RANK() OVER (SELECT * FROM Employees ORDER BY Salary)
FROM Employees
So, effectively, I want to say, "Give me the percentile of this record when compared to the entire table."
In the above case, it's one and the same because I'l retrieving the entire table. But I can't pull a single record and gets its percentile rank for the table as a whole.
SELECT Name, Salary
PERCENT_RANK() OVER (ORDER BY Salary)
FROM Employees
WHERE id = 1234
That one record will always be 1.000
because it's at the 100% percentile for all the rows returned, which is just the one.
SELECT e.Name, e.Salary, r.Employee_Rank
FROM Employees e
JOIN (
SELECT id,
PERCENT_RANK() OVER (ORDER BY Salary) AS Employee_Rank
FROM Employees
) r ON r.Id = e.Id
WHERE e.id = 1234