Input Table is
| Target | Agent | RankA |
|---|---|---|
| 15000 | A | null |
| 15000 | A | null |
| 14500 | A | null |
required output is
| Target | Agent | RankA |
|---|---|---|
| 15000 | A | 1 |
| 15000 | A | 1 |
| 14500 | A | 3 |
as first record is 15000, so its rank is 1
as second record is 15000, so its rank is also 1
as third record is 14500, so its rank is also 3 and not 2 because even though first two records have same record, the count is now 2 so new rank will be 3 for third record
need query for the same..
here is script for data preparation
SELECT 15000 TARGET,
'' AS AGENT,
NULL RANKA
INTO #TABLEA;
INSERT INTO #TABLEA
VALUES (1400, 'B', NULL);
>Solution :
You can do it using rank() to get the rank, and rank/count to get Percentile
with cte as (
select Target, Agent, rank() over (partition by Agent order by Target desc) as Ranka
from TableA
)
select Target, Agent, cast(Ranka as float)/CAST(count(*) over (partition by Agent) AS FLOAT)*100 as Percentile
from cte
Or this if you dont need Percentile :
select Target, Agent, rank() over (partition by Agent order by Target desc) as Ranka
from TableA