SlideShare a Scribd company logo
1 of 2
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
-- 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

More Related Content

Recently uploaded

Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 

Recently uploaded (20)

Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
1111 ChatGPT Prompts PDF Free Download - Prompts for ChatGPT
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
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