This solution show how rank records in one table splitting places due to group / column values. In my example we have table with agents and his region description. In the third column the results of the agent. Higher place them higher agent results. Separate place for each region, which will be the first place for the USA, for the EU and for the Other.
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Sql: rank records in one table splitting places due to group
1. This solution show how rank records in one table splitting places due to group
/ column values. In my example we have table with agents and his region
description. In the third column the results of the agent. Higher place them
higher agent results. Separate place for each region, which will be the first
place for the USA, for the EU and for the Other.
1. Preparing
-- creating table
CREATE TABLE [dbo].[rank](
[agent] [int] NULL,
[region] [nvarchar](50) NULL,
[result] [float] NULL
) ON [PRIMARY]
-- insert some example values
INSERT INTO [test].[dbo].[rank]
SELECT 101,'USA',3423.3 UNION ALL
SELECT 102,'USA',386 UNION ALL
SELECT 103,'USA',458 UNION ALL
SELECT 104,'EU',3423 UNION ALL
SELECT 105,'EU',3423 UNION ALL
SELECT 106,'EU',5000 UNION ALL
SELECT 107,'USA',342 UNION ALL
SELECT 108,'Other',3121 UNION ALL
SELECT 109,'Other',1232 UNION ALL
SELECT 110,'Other',1232 UNION ALL
SELECT 111,'Other',2313 UNION ALL
SELECT 113,'EU',342 UNION ALL
SELECT 112,'Other',1233
2. Solution
-- base solution (one select statement)
-- ============================================
SELECT x.agent, x.region, x.result, x.place-x.min_place+1 as place FROM (
SELECT r.agent, r.region, r.result, ROW_NUMBER() OVER(ORDER BY
r.region,r.result DESC) as place, r1.min_place
FROM [test].[dbo].[rank] as r
inner join
(
SELECT region, MIN(place) as min_place
FROM
(
SELECT r.agent, r.region, r.result, ROW_NUMBER() OVER(ORDER BY
r.region,r.result DESC) as place
FROM [test].[dbo].[rank] as r
) as a
GROUP BY region) as r1 on r.region=r1.region
) as x
2. -- solution of the unsolved problem of ex aequo places (one select statement)
-- ============================================
SELECT x.agent, x.region, x.result, CASE WHEN x.c_result=1 THEN x.placex.min_place+1 ELSE x.min_place_result-x.min_place+1 END as place FROM (
SELECT r.agent, r.region, r.result, ROW_NUMBER() OVER(ORDER BY
r.region,r.result DESC) as place, r1.min_place, r2.c_result,
r2.min_place_result
FROM [test].[dbo].[rank] as r
inner join
(
SELECT region, MIN(place) as min_place
FROM
(
SELECT r.agent, r.region, r.result, ROW_NUMBER() OVER(ORDER BY
r.region,r.result DESC) as place
FROM [test].[dbo].[rank] as r
) as a
GROUP BY region) as r1 on r.region=r1.region
left outer join
(
SELECT region, result, COUNT(*) as c_result, MIN(place) as min_place_result
FROM
(
SELECT r.agent, r.region, r.result, ROW_NUMBER() OVER(ORDER BY
r.region,r.result DESC) as place
FROM [test].[dbo].[rank] as r
) as a
GROUP BY region, result) as r2 ON r2.region=r.region and r2.result=r.result
) as x