Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Oracle SQL: Using the analytic rank function

## Expert Karen Morton explains how to use a simple analytic rank function to sort through a group of golf scores.

I have an application that records golf scores in a golf tournament and I'm trying to write SQL to determine the players for 1st, 2nd and 3rd places based on score.

Let's say the table includes:

create table player(player_id number,tournament_id, score number);
create table (player_id number, handicap number);
create tournament (tournament_id number, 1st_place_dollars number(5,2), 2nd_place_dollars number(5,2), 3rd_place_dollars number(5,2));

The number of players could range from 4 to 100 (or more)

The scores could be in any combination, for example:

Player1, 68  wins 1st
Player2, 69  wins 2nd
Player3, 70  wins 3rd
Player4, 72
etc.

The problem is when ties come into play.

example 1
player1 68 wins 1st
player2 69 wins 1/2 of (second + thrid place)
player3 69 wins 1/2 of (second + third place)
player4 70
etc

example 2
player1 68 wins 1st
player2 69 wins 1/3 of (second + thrid place)
player3 69 wins 1/3 of (second + third place)
player4 69 wins 1/3 of (second + third place)
player5 70
etc

example 3
player1 68 wins 1st
player2 69 wins 1/4 of (second + thrid place)
player3 69 wins 1/4 of (second + third place)
player4 69 wins 1/4 of (second + third place)
player5 69 wins 1/4 of (second + third place)
player6 70
etc

example 4  (as you can this could continue for ties)
player1 68 wins 1st
player2 69 wins 1/5 of (second + thrid place)
player3 69 wins 1/5 of (second + third place)
player4 69 wins 1/5 of (second + third place)
player5 69 wins 1/5 of (second + third place)
player6 69 wins 1/5 of (second + third place)
player7 70
etc

example 5 (in this example only for ties for 1st place)
The player with the lowest handicap will win 1st and then the others will win 2nd or split 2nd and 3rd place)
player1 68
player2 68
player3 69
player4 70

I'm trying to put this SQL into a stored procedure. I can do all this using IF, ELSEIF, ELSE or CASE statements but it takes a lot of code.  Is there a simple RANK using greatest and lowest to come up with appropriate ranking?

You're looking for the analytic rank function. Here's a simple example:

create table player(player_id number,tournament_id number, score number);
insert into player values (1,1,68);
insert into player values (2,1,69);
insert into player values (3,1,69);
insert into player values (4,1,69);
insert into player values (5,1,70);
insert into player values (6,1,70);
insert into player values (7,1,71);
insert into player values (8,1,72);
insert into player values (9,1,72);
insert into player values (10,1,73);
insert into player values (11,1,73);
insert into player values (12,1,73);
insert into player values (13,1,74);
insert into player values (14,1,75);
commit;

SQL> select player_id, rank() over (partition by tournament_id order by score)  as place
2  from player
3  order by place, player_id ;
PLAYER_ID      PLACE
---------- ----------
1          1
2          2
3          2
4          2
5          5
6          5
7          7
8          8
9          8
10         10
11         10
12         10
13         13
14         14

The dense_rank function would not skip places but would allow you to have multiple people in a single place followed by the next place as follows:

SQL> select player_id, dense_rank() over (partition by tournament_id order by score)  as place
2  from player
3  order by place, player_id ;
PLAYER_ID      PLACE
---------- ----------
1          1
2          2
3          2
4          2
5          3
6          3
7          4
8          5
9          5
10          6
11          6
12          6
13          7
14          8

### 1 comment

Send me notifications when other members comment.
If you need help understanding how analytic functions work, this is for you:

Understanding Analytic Functions in Oracle SQL
Cancel

## SearchDataManagement

• ### PlanetScale extends multi-cloud database as a service

Open source-based PlanetScaleDB adds support for Microsoft Azure, enabling a multi-cloud database as a service platform that ...

• ### Oracle NoSQL database comes to the cloud

Another Oracle database is coming to the cloud with the general availability of Oracle NoSQL Database Cloud Service as the demand...

• ### MariaDB SkySQL enables cloud-native database as a service

MariaDB brings back an old name for its cloud-native database-as-a-service platform. MariaDB CEO Michael Howard details how he's ...

• ### Importance of analytics clear amid COVID-19 crisis

With health organizations and business leaders alike trying to minimize the effects of COVID-19, analytics is playing a critical ...

• ### AI tools in analytics software key in fighting COVID-19

With data skewed by incomplete testing, augmented intelligence and machine learning tools are helping data scientists create ...

• ### Should your company hire a freelance data scientist?

Enterprises need to make the right hiring choices when it comes to supporting their data projects. Here are some reasons your ...

## SearchSAP

• ### SAP S/4HANA deployment options: Legacy ERP vs. cloud

Take this tour of the on-premises S/4HANA and its two cloud siblings and get expert advice on major differences in cost and ...

• ### Floating the possibilities of SAP S/4HANA Cloud migration

When weighing the advantages of abandoning legacy ERP systems for SAP S/4HANA Cloud, factors include implementation and support ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Eclipse launches Theia 1.0 as alternative to VS Code

The Eclipse Foundation's Theia framework for building desktop and cloud IDEs has the potential to be as big as the initial ...

• ### Developing a COVID-19 application? Design it the right way

Application developers with skills and an idea could design an app to combat the virus. Follow these insights to effectively ...

• ### How developers can volunteer during the COVID-19 pandemic

What resources can developers offer to go against COVID-19? Explore some promising developer volunteer and open source project ...

## SearchDataCenter

• ### Build a migration plan for enterprise edge infrastructure

Micro data centers are essential to edge computing use cases. Build an effective migration plan through site selection, ...

• ### Tech giants aim supercomputers, clouds at finding a COVID-19 cure

The new COVID-19 HPC consortium comprised of tech giants, national labs and academia are using supercomputers and clouds to speed...

• ### Top vendor SDDC certification programs for admins

If you decide to grow your knowledge of software-defined data center technologies, VMware, Nuage Networks and Microsoft offer ...

## SearchContentManagement

• ### Creating digital experiences key to driving exceptional CX

The way in which businesses build digital experiences is ever-evolving. It's a race to create easier-to-use customer interfaces ...

• ### Vendors offer free remote work technology for telecommuting

It's more than a marketing ploy: Cloud technology vendors, seeing their own issues enabling remote work, extend paid products to ...

• ### How businesses should deal with enterprise search issues

Enterprise search issues frequently complicate user experience with ECM systems. While users may face many problems, they also ...

## SearchHRSoftware

• ### How to create an employee journey map

This handy guide shows you how to build your own employee journey map, which is more important than ever as the coronavirus ...

• ### Remote team tech Sneek keeps co-workers in view

Sneek, a remote team platform, uses frequently updated photographs of remote workers to connect co-workers together.

• ### Best-case, worst-case pandemic scenarios for India service providers

India's IT and business support services will be tested during that country's pandemic lockdown. Many workers are now operating ...

Close