Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need query to get data as per below

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Demo here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading