SlideShare a Scribd company logo
SQL 2005 - The ranking functions
The ranking functions – Step 1
USE tempdb;
GO
IF OBJECT_ID('dbo.Orders') IS NOT NULL
DROP TABLE dbo.Orders;
GO
The ranking functions – Step 2
CREATE TABLE dbo.Orders
( orderid INT NOT NULL,
orderdate DATETIME NOT NULL,
empid INT NOT NULL,
custid VARCHAR(5) NOT NULL,
qty INT NOT NULL,
CONSTRAINT PK_Orders PRIMARY KEY
NONCLUSTERED(orderid)
);
The ranking functions – Step 3
CREATE UNIQUE CLUSTERED INDEX
idx_UC_orderdate_orderid
ON dbo.Orders(orderdate, orderid);
The ranking functions – Step 4
SET NOCOUNT ON;
INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
VALUES(30001, '20030802', 3, 'B', 10);
INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
VALUES(10001, '20031224', 1, 'C', 10);
INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
VALUES(10005, '20031224', 1, 'A', 30);
INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
VALUES(40001, '20040109', 4, 'A', 40);
INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty)
VALUES(10006, '20040118', 1, 'C', 10);
The ranking functions – Step 5
-- Analytical Ranking Functions
–- without PARTITION BY clause
 
SELECT orderid, qty,
  ROW_NUMBER() OVER(ORDER BY qty) AS rownum,
  RANK()       OVER(ORDER BY qty) AS rnk,
  DENSE_RANK() OVER(ORDER BY qty) AS densernk,
  NTILE(4)     OVER(ORDER BY qty) AS ntile4
FROM dbo.Orders
ORDER BY qty;
GO
The ranking function - Output
Ordered Qty Rownum Rnk Densernk Ntile4
30001 10 1 1 1 1
10001 10 2 1 1 1
10006 10 3 1 1 2
10005 30 4 4 2 3
40001 40 5 5 3 4

More Related Content

PPTX
Communicating Intention with Functional TypeScript
PDF
New tsql features
PPTX
PDF
New Query Optimizer features in MariaDB 10.3
ODP
Building a Hierarchical Data Model Using the Latest IBM Informix Features
PDF
Informatics Practices/ Information Practices Project (IP Project Class 12)
PDF
T sql denali code Day of .Net
PPTX
Adv.+SQL+PPT+final.pptx
Communicating Intention with Functional TypeScript
New tsql features
New Query Optimizer features in MariaDB 10.3
Building a Hierarchical Data Model Using the Latest IBM Informix Features
Informatics Practices/ Information Practices Project (IP Project Class 12)
T sql denali code Day of .Net
Adv.+SQL+PPT+final.pptx

Similar to Sql 2005 the ranking functions (14)

PPTX
Exploring Advanced SQL Techniques Using Analytic Functions
PPTX
Exploring Advanced SQL Techniques Using Analytic Functions
PDF
Oracle Advanced SQL and Analytic Functions
PDF
Oracle_Analytical_function.pdf
PPT
INTRODUCTION TO SQL QUERIES REALTED BRIEF
PDF
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
PPTX
The Five Best Things To Happen To SQL
PDF
Window functions for Data Science
PPT
Enabling Applications with Informix' new OLAP functionality
PPSX
Analytic & Windowing functions in oracle
PPTX
Analytical Functions for DWH
PPTX
Veri Ambarları için Oracle'ın Analitik SQL Desteği
PPT
Olap Functions Suport in Informix
RTF
Sql functions
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
Oracle Advanced SQL and Analytic Functions
Oracle_Analytical_function.pdf
INTRODUCTION TO SQL QUERIES REALTED BRIEF
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
The Five Best Things To Happen To SQL
Window functions for Data Science
Enabling Applications with Informix' new OLAP functionality
Analytic & Windowing functions in oracle
Analytical Functions for DWH
Veri Ambarları için Oracle'ın Analitik SQL Desteği
Olap Functions Suport in Informix
Sql functions
Ad

More from rchakra (12)

PPTX
Requirement management presentation to a software team
PPT
Subversion client
PPT
Subversion
PPT
Sql basics 2
PPT
T-Sql basics
PPT
Sql architecture
PPT
Introduction to ADO.NET
PPTX
Intro to Microsoft.NET
PPT
Object oriented programming systems
PPT
Subversion Admin
PPT
Intro to UML 2
PPT
Intro To .Net Threads
Requirement management presentation to a software team
Subversion client
Subversion
Sql basics 2
T-Sql basics
Sql architecture
Introduction to ADO.NET
Intro to Microsoft.NET
Object oriented programming systems
Subversion Admin
Intro to UML 2
Intro To .Net Threads
Ad

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
KodekX | Application Modernization Development
PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
Teaching material agriculture food technology
PDF
Electronic commerce courselecture one. Pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Big Data Technologies - Introduction.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
Understanding_Digital_Forensics_Presentation.pptx
KodekX | Application Modernization Development
Network Security Unit 5.pdf for BCA BBA.
Teaching material agriculture food technology
Electronic commerce courselecture one. Pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Dropbox Q2 2025 Financial Results & Investor Presentation
The Rise and Fall of 3GPP – Time for a Sabbatical?
20250228 LYD VKU AI Blended-Learning.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Machine learning based COVID-19 study performance prediction
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Empathic Computing: Creating Shared Understanding
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Big Data Technologies - Introduction.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
MIND Revenue Release Quarter 2 2025 Press Release

Sql 2005 the ranking functions

  • 1. SQL 2005 - The ranking functions
  • 2. The ranking functions – Step 1 USE tempdb; GO IF OBJECT_ID('dbo.Orders') IS NOT NULL DROP TABLE dbo.Orders; GO
  • 3. The ranking functions – Step 2 CREATE TABLE dbo.Orders ( orderid INT NOT NULL, orderdate DATETIME NOT NULL, empid INT NOT NULL, custid VARCHAR(5) NOT NULL, qty INT NOT NULL, CONSTRAINT PK_Orders PRIMARY KEY NONCLUSTERED(orderid) );
  • 4. The ranking functions – Step 3 CREATE UNIQUE CLUSTERED INDEX idx_UC_orderdate_orderid ON dbo.Orders(orderdate, orderid);
  • 5. The ranking functions – Step 4 SET NOCOUNT ON; INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty) VALUES(30001, '20030802', 3, 'B', 10); INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty) VALUES(10001, '20031224', 1, 'C', 10); INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty) VALUES(10005, '20031224', 1, 'A', 30); INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty) VALUES(40001, '20040109', 4, 'A', 40); INSERT INTO dbo.Orders(orderid, orderdate, empid, custid, qty) VALUES(10006, '20040118', 1, 'C', 10);
  • 6. The ranking functions – Step 5 -- Analytical Ranking Functions –- without PARTITION BY clause   SELECT orderid, qty,   ROW_NUMBER() OVER(ORDER BY qty) AS rownum,   RANK()       OVER(ORDER BY qty) AS rnk,   DENSE_RANK() OVER(ORDER BY qty) AS densernk,   NTILE(4)     OVER(ORDER BY qty) AS ntile4 FROM dbo.Orders ORDER BY qty; GO
  • 7. The ranking function - Output Ordered Qty Rownum Rnk Densernk Ntile4 30001 10 1 1 1 1 10001 10 2 1 1 1 10006 10 3 1 1 2 10005 30 4 4 2 3 40001 40 5 5 3 4