DENSE RANK SQL function
1. Usage of DENSE_RANK to show cumulative rank distribution of orders.
SQL Server Query 1
WITH CustomerOrderCounts AS (
SELECT
c.CustomerID,
c.CompanyName,
c.Country,
COUNT(o.OrderID) AS TotalOrders
FROM
Customers c
LEFT JOIN
Orders o ON c.CustomerID = o.CustomerID
GROUP BY
c.CustomerID,
c.CompanyName,
c.Country
),
OrderRankWithinCountry AS (
SELECT
CustomerID,
CompanyName,
Country,
TotalOrders,
DENSE_RANK() OVER (PARTITION BY Country ORDER BY TotalOrders DESC) AS OrderRank
FROM
CustomerOrderCounts
),
RankDistributionWithinCountry AS (
SELECT
Country,
OrderRank,
COUNT(*) AS NumberOfCustomersAtRank,
CUME_DIST() OVER (PARTITION BY Country ORDER BY OrderRank) AS CumulativeRankDistribution
FROM
OrderRankWithinCountry
GROUP BY
Country,
OrderRank
)
SELECT
orc.CustomerID,
orc.CompanyName,
orc.Country,
orc.TotalOrders,
orc.OrderRank,
rdc.NumberOfCustomersAtRank,
rdc.CumulativeRankDistribution
FROM
OrderRankWithinCountry orc
JOIN
RankDistributionWithinCountry rdc ON orc.Country = rdc.Country AND orc.OrderRank = rdc.OrderRank
ORDER BY
orc.Country,
orc.OrderRank,
orc.TotalOrders DESC;
Create SQL query with SqlQueryBuilder 1
var (sql1, parameters1) = new SqlQueryBuilder()
.WithCTETable(new Table("CustomerOrderCounts"), new SqlQueryBuilder()
.Select()
.Columns("c.CustomerID", "c.CompanyName", "c.Country")
.Column(new COUNT(new Column("o.OrderID")), "TotalOrders")
.From("Customers", "c")
.Join(new List<IJoin>()
{
new LEFTJOIN().TableName(new Table("Orders","o"))
.On(new Column("c.CustomerID").Equale(new Column("o.CustomerID")))
})
.GroupBy(new GroupBy("c.CustomerID", "c.CompanyName", "c.Country"))
)
.WithCTETable(new Table("OrderRankWithinCountry"), new SqlQueryBuilder()
.Select()
.Columns("CustomerID", "CompanyName", "Country", "TotalOrders")
.Column(new DENSE_RANK().PARTITION_BY(new Column("Country")).ORDER_BY(new OrderBy().SetColumnAscending("TotalOrders")), "OrderRank")
.From("CustomerOrderCounts")
)
.WithCTETable(new Table("RankDistributionWithinCountry"), new SqlQueryBuilder()
.Select()
.Columns("Country", "OrderRank")
.Column(new COUNT(new Column("Country")), "NumberOfCustomersAtRank")
.Column(new CUME_DIST().PARTITION_BY(new Column("Country"))
.ORDER_BY(new OrderBy().SetColumnAscending("OrderRank")), "CumulativeRankDistribution")
.From("OrderRankWithinCountry")
.GroupBy(new GroupBy("Country", "OrderRank"))
)
.Select()
.Columns("orc.CustomerID", "orc.CompanyName", "orc.Country", "orc.TotalOrders", "orc.OrderRank", "rdc.NumberOfCustomersAtRank", "rdc.CumulativeRankDistribution")
.From("OrderRankWithinCountry", "orc")
.Join(new List<IJoin>()
{
new INNERJOIN().TableName(new Table("RankDistributionWithinCountry","rdc"))
.On(new Column("orc.Country").Equale(new Column("rdc.Country"))
.AND(new Column("orc.OrderRank").Equale(new Column("rdc.OrderRank")))
})
.OrderBy(new OrderBy().SetColumnAscending("orc.Country")
.SetColumnAscending("orc.OrderRank")
.SetColumnDescending("orc.TotalOrders"))
.Build();
Query build by SqlQueryBuilder 1
WITH CustomerOrderCounts
AS (SELECT c.CustomerID,
c.CompanyName,
c.Country,
COUNT(o.OrderID) AS TotalOrders
FROM Customers AS c
LEFT OUTER JOIN
Orders AS o
ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerID, c.CompanyName, c.Country),
OrderRankWithinCountry
AS (SELECT CustomerID,
CompanyName,
Country,
TotalOrders,
DENSE_RANK() OVER (PARTITION BY Country ORDER BY TotalOrders ASC) AS OrderRank
FROM CustomerOrderCounts),
RankDistributionWithinCountry
AS (SELECT Country,
OrderRank,
COUNT(Country) AS NumberOfCustomersAtRank,
CUME_DIST() OVER (PARTITION BY Country ORDER BY OrderRank ASC) AS CumulativeRankDistribution
FROM OrderRankWithinCountry
GROUP BY Country, OrderRank)
SELECT orc.CustomerID,
orc.CompanyName,
orc.Country,
orc.TotalOrders,
orc.OrderRank,
rdc.NumberOfCustomersAtRank,
rdc.CumulativeRankDistribution
FROM OrderRankWithinCountry AS orc
INNER JOIN
RankDistributionWithinCountry AS rdc
ON orc.Country = rdc.Country
AND orc.OrderRank = rdc.OrderRank
ORDER BY orc.Country ASC, orc.OrderRank ASC, orc.TotalOrders DESC;
Parameters (If used)
Query Results 1:
|
CustomerID |
CompanyName |
Country |
TotalOrders |
OrderRank |
NumberOfCustomersAtRank |
CumulativeRankDistribution |
1 |
OCEAN
|
Océano Atlántico Ltda.
|
Argentina
|
5
|
1
|
2
|
0.5
|
2 |
RANCH
|
Rancho grande
|
Argentina
|
5
|
1
|
2
|
0.5
|
3 |
CACTU
|
Cactus Comidas para llevar
|
Argentina
|
6
|
2
|
1
|
1
|
4 |
PICCO
|
Piccolo und mehr
|
Austria
|
10
|
1
|
1
|
0.5
|
5 |
ERNSH
|
Ernst Handel
|
Austria
|
30
|
2
|
1
|
1
|
6 |
MAISD
|
Maison Dewey
|
Belgium
|
7
|
1
|
1
|
0.5
|
7 |
SUPRD
|
Suprêmes délices
|
Belgium
|
12
|
2
|
1
|
1
|
8 |
COMMI
|
Comércio Mineiro
|
Brazil
|
5
|
1
|
1
|
0.142857142857143
|
9 |
TRADH
|
Tradição Hipermercados
|
Brazil
|
6
|
2
|
1
|
0.285714285714286
|
10 |
FAMIA
|
Familia Arquibaldo
|
Brazil
|
7
|
3
|
1
|
0.428571428571429
|
11 |
GOURL
|
Gourmet Lanchonetes
|
Brazil
|
9
|
4
|
3
|
0.571428571428571
|
12 |
QUEDE
|
Que Delícia
|
Brazil
|
9
|
4
|
3
|
0.571428571428571
|
13 |
WELLI
|
Wellington Importadora
|
Brazil
|
9
|
4
|
3
|
0.571428571428571
|
14 |
RICAR
|
Ricardo Adocicados
|
Brazil
|
11
|
5
|
1
|
0.714285714285714
|
15 |
QUEEN
|
Queen Cozinha
|
Brazil
|
13
|
6
|
1
|
0.857142857142857
|
16 |
HANAR
|
Hanari Carnes
|
Brazil
|
14
|
7
|
1
|
1
|
17 |
LAUGB
|
Laughing Bacchus Wine Cellars
|
Canada
|
3
|
1
|
1
|
0.333333333333333
|
18 |
MEREP
|
Mère Paillarde
|
Canada
|
13
|
2
|
1
|
0.666666666666667
|
19 |
BOTTM
|
Bottom-Dollar Markets
|
Canada
|
14
|
3
|
1
|
1
|
20 |
SIMOB
|
Simons bistro
|
Denmark
|
7
|
1
|
1
|
0.5
|
21 |
VAFFE
|
Vaffeljernet
|
Denmark
|
11
|
2
|
1
|
1
|
22 |
WILMK
|
Wilman Kala
|
Finland
|
7
|
1
|
1
|
0.5
|
23 |
WARTH
|
Wartian Herkku
|
Finland
|
15
|
2
|
1
|
1
|
24 |
PARIS
|
Paris spécialités
|
France
|
0
|
1
|
1
|
0.125
|
25 |
FRANR
|
France restauration
|
France
|
3
|
2
|
1
|
0.25
|
26 |
DUMON
|
Du monde entier
|
France
|
4
|
3
|
3
|
0.375
|
27 |
LACOR
|
La corne d'abondance
|
France
|
4
|
3
|
3
|
0.375
|
28 |
SPECD
|
Spécialités du monde
|
France
|
4
|
3
|
3
|
0.375
|
29 |
VINET
|
Vins et alcools Chevalier
|
France
|
5
|
4
|
2
|
0.5
|
30 |
FOLIG
|
Folies gourmandes
|
France
|
5
|
4
|
2
|
0.5
|
31 |
VICTE
|
Victuailles en stock
|
France
|
10
|
5
|
1
|
0.625
|
32 |
BLONP
|
Blondesddsl père et fils
|
France
|
11
|
6
|
1
|
0.75
|
33 |
LAMAI
|
La maison d'Asie
|
France
|
14
|
7
|
1
|
0.875
|
34 |
BONAP
|
Bon app'
|
France
|
17
|
8
|
1
|
1
|
35 |
MORGK
|
Morgenstern Gesundkost
|
Germany
|
5
|
1
|
1
|
0.142857142857143
|
36 |
DRACD
|
Drachenblut Delikatessen
|
Germany
|
6
|
2
|
3
|
0.285714285714286
|
37 |
ALFKI
|
Alfreds Futterkiste
|
Germany
|
6
|
2
|
3
|
0.285714285714286
|
38 |
TOMSP
|
Toms Spezialitäten
|
Germany
|
6
|
2
|
3
|
0.285714285714286
|
39 |
BLAUS
|
Blauer See Delikatessen
|
Germany
|
7
|
3
|
1
|
0.428571428571429
|
40 |
OTTIK
|
Ottilies Käseladen
|
Germany
|
10
|
4
|
2
|
0.571428571428571
|
41 |
WANDK
|
Die Wandernde Kuh
|
Germany
|
10
|
4
|
2
|
0.571428571428571
|
42 |
KOENE
|
Königlich Essen
|
Germany
|
14
|
5
|
1
|
0.714285714285714
|
43 |
LEHMS
|
Lehmanns Marktstand
|
Germany
|
15
|
6
|
2
|
0.857142857142857
|
44 |
FRANK
|
Frankenversand
|
Germany
|
15
|
6
|
2
|
0.857142857142857
|
45 |
QUICK
|
QUICK-Stop
|
Germany
|
28
|
7
|
1
|
1
|
46 |
HUNGO
|
Hungry Owl All-Night Grocers
|
Ireland
|
19
|
1
|
1
|
1
|
47 |
FRANS
|
Franchi S.p.A.
|
Italy
|
6
|
1
|
1
|
0.333333333333333
|
48 |
MAGAA
|
Magazzini Alimentari Riuniti
|
Italy
|
10
|
2
|
1
|
0.666666666666667
|
49 |
REGGC
|
Reggiani Caseifici
|
Italy
|
12
|
3
|
1
|
1
|
50 |
CENTC
|
Centro comercial Moctezuma
|
Mexico
|
1
|
1
|
1
|
0.2
|
51 |
ANATR
|
Ana Trujillo Emparedados y helados
|
Mexico
|
4
|
2
|
1
|
0.4
|
52 |
PERIC
|
Pericles Comidas clásicas
|
Mexico
|
6
|
3
|
1
|
0.6
|
53 |
ANTON
|
Antonio Moreno Taquería
|
Mexico
|
7
|
4
|
1
|
0.8
|
54 |
TORTU
|
Tortuga Restaurante
|
Mexico
|
10
|
5
|
1
|
1
|
55 |
SANTG
|
Santé Gourmet
|
Norway
|
6
|
1
|
1
|
1
|
56 |
WOLZA
|
Wolski Zajazd
|
Poland
|
7
|
1
|
1
|
1
|
57 |
PRINI
|
Princesa Isabel Vinhos
|
Portugal
|
5
|
1
|
1
|
0.5
|
58 |
FURIB
|
Furia Bacalhau e Frutos do Mar
|
Portugal
|
8
|
2
|
1
|
1
|
59 |
FISSA
|
FISSA Fabrica Inter. Salchichas S.A.
|
Spain
|
0
|
1
|
1
|
0.25
|
60 |
BOLID
|
Bólido Comidas preparadas
|
Spain
|
3
|
2
|
1
|
0.5
|
61 |
GALED
|
Galería del gastrónomo
|
Spain
|
5
|
3
|
2
|
0.75
|
62 |
ROMEY
|
Romero y tomillo
|
Spain
|
5
|
3
|
2
|
0.75
|
63 |
GODOS
|
Godos Cocina Típica
|
Spain
|
10
|
4
|
1
|
1
|
64 |
BERGS
|
Berglunds snabbköp
|
Sweden
|
18
|
1
|
1
|
0.5
|
65 |
FOLKO
|
Folk och fä HB
|
Sweden
|
19
|
2
|
1
|
1
|
66 |
CHOPS
|
Chop-suey Chinese
|
Switzerland
|
8
|
1
|
1
|
0.5
|
67 |
RICSU
|
Richter Supermarkt
|
Switzerland
|
10
|
2
|
1
|
1
|
68 |
NORTS
|
North/South
|
UK
|
3
|
1
|
2
|
0.2
|
69 |
CONSH
|
Consolidated Holdings
|
UK
|
3
|
1
|
2
|
0.2
|
70 |
EASTC
|
Eastern Connection
|
UK
|
8
|
2
|
1
|
0.4
|
71 |
SEVES
|
Seven Seas Imports
|
UK
|
9
|
3
|
1
|
0.6
|
72 |
BSBEV
|
B's Beverages
|
UK
|
10
|
4
|
2
|
0.8
|
73 |
ISLAT
|
Island Trading
|
UK
|
10
|
4
|
2
|
0.8
|
74 |
AROUT
|
Around the Horn
|
UK
|
13
|
5
|
1
|
1
|
75 |
LAZYK
|
Lazy K Kountry Store
|
USA
|
2
|
1
|
1
|
0.0909090909090909
|
76 |
THECR
|
The Cracker Box
|
USA
|
3
|
2
|
2
|
0.181818181818182
|
77 |
TRAIH
|
Trail's Head Gourmet Provisioners
|
USA
|
3
|
2
|
2
|
0.181818181818182
|
78 |
THEBI
|
The Big Cheese
|
USA
|
4
|
3
|
2
|
0.272727272727273
|
79 |
LETSS
|
Let's Stop N Shop
|
USA
|
4
|
3
|
2
|
0.272727272727273
|
80 |
HUNGC
|
Hungry Coyote Import Store
|
USA
|
5
|
4
|
1
|
0.363636363636364
|
81 |
LONEP
|
Lonesome Pine Restaurant
|
USA
|
8
|
5
|
1
|
0.454545454545454
|
82 |
SPLIR
|
Split Rail Beer & Ale
|
USA
|
9
|
6
|
1
|
0.545454545454545
|
83 |
OLDWO
|
Old World Delicatessen
|
USA
|
10
|
7
|
1
|
0.636363636363636
|
84 |
GREAL
|
Great Lakes Food Market
|
USA
|
11
|
8
|
1
|
0.727272727272727
|
85 |
WHITC
|
White Clover Markets
|
USA
|
14
|
9
|
1
|
0.818181818181818
|
86 |
RATTC
|
Rattlesnake Canyon Grocery
|
USA
|
18
|
10
|
1
|
0.909090909090909
|
87 |
SAVEA
|
Save-a-lot Markets
|
USA
|
31
|
11
|
1
|
1
|
88 |
GROSR
|
GROSELLA-Restaurante
|
Venezuela
|
2
|
1
|
1
|
0.25
|
89 |
LINOD
|
LINO-Delicateses
|
Venezuela
|
12
|
2
|
1
|
0.5
|
90 |
LILAS
|
LILA-Supermercado
|
Venezuela
|
14
|
3
|
1
|
0.75
|
91 |
HILAA
|
HILARION-Abastos
|
Venezuela
|
18
|
4
|
1
|
1
|