Window COUNT SQL function
1. Usage of COUNT to find customer distribution within country
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
),
CountryOrderDistribution AS (
SELECT
Country,
TotalOrders,
CUME_DIST() OVER (PARTITION BY Country ORDER BY TotalOrders) + 1 AS CumulativeDistributionWithinCountry
FROM
CustomerOrderCounts
)
SELECT
coc.CustomerID,
coc.CompanyName,
coc.Country,
coc.TotalOrders,
cod.CumulativeDistributionWithinCountry
FROM
CustomerOrderCounts coc
JOIN
CountryOrderDistribution cod ON coc.Country = cod.Country AND coc.TotalOrders = cod.TotalOrders
ORDER BY
coc.Country,
cod.CumulativeDistributionWithinCountry;
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("CountryOrderDistribution"), new SqlQueryBuilder()
.Select()
.Columns("Country", "TotalOrders")
.Column(new CUME_DIST().PARTITION_BY(new Column("Country")).ORDER_BY(new OrderBy().Set(new Column("TotalOrders"))
).AddArithmatic(new Arithmatic().Add(1)), "CumulativeDistributionWithinCountry")
.From("CustomerOrderCounts")
)
.Select()
.Columns("coc.CustomerID", "coc.CompanyName", "coc.Country", "coc.TotalOrders", "cod.CumulativeDistributionWithinCountry")
.From("CustomerOrderCounts", "coc")
.Join(new List<IJoin>()
{
new INNERJOIN().TableName(new Table("CountryOrderDistribution","cod"))
.On(new Column("coc.Country").Equale(new Column("cod.Country")))
.AND(new Column("coc.TotalOrders").Equale(new Column("cod.TotalOrders")))
})
.OrderBy(new OrderBy().Set(new Column("coc.Country")).Set(new Column("cod.CumulativeDistributionWithinCountry")))
.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),
CountryOrderDistribution
AS (SELECT Country,
TotalOrders,
CUME_DIST() OVER (PARTITION BY Country ORDER BY TotalOrders ASC) + @pMAIN_2507192107352674730 AS CumulativeDistributionWithinCountry
FROM CustomerOrderCounts)
SELECT coc.CustomerID,
coc.CompanyName,
coc.Country,
coc.TotalOrders,
cod.CumulativeDistributionWithinCountry
FROM CustomerOrderCounts AS coc
INNER JOIN
CountryOrderDistribution AS cod
ON coc.Country = cod.Country
AND coc.TotalOrders = cod.TotalOrders
ORDER BY coc.Country ASC, cod.CumulativeDistributionWithinCountry ASC;
Parameters (If used)
Name |
Value |
@pMAIN_2507192107352674730 |
1 |
Query Results 1:
|
CustomerID |
CompanyName |
Country |
TotalOrders |
CumulativeDistributionWithinCountry |
1 |
OCEAN
|
Océano Atlántico Ltda.
|
Argentina
|
5
|
1.66666666666667
|
2 |
OCEAN
|
Océano Atlántico Ltda.
|
Argentina
|
5
|
1.66666666666667
|
3 |
RANCH
|
Rancho grande
|
Argentina
|
5
|
1.66666666666667
|
4 |
RANCH
|
Rancho grande
|
Argentina
|
5
|
1.66666666666667
|
5 |
CACTU
|
Cactus Comidas para llevar
|
Argentina
|
6
|
2
|
6 |
PICCO
|
Piccolo und mehr
|
Austria
|
10
|
1.5
|
7 |
ERNSH
|
Ernst Handel
|
Austria
|
30
|
2
|
8 |
MAISD
|
Maison Dewey
|
Belgium
|
7
|
1.5
|
9 |
SUPRD
|
Suprêmes délices
|
Belgium
|
12
|
2
|
10 |
COMMI
|
Comércio Mineiro
|
Brazil
|
5
|
1.11111111111111
|
11 |
TRADH
|
Tradição Hipermercados
|
Brazil
|
6
|
1.22222222222222
|
12 |
FAMIA
|
Familia Arquibaldo
|
Brazil
|
7
|
1.33333333333333
|
13 |
GOURL
|
Gourmet Lanchonetes
|
Brazil
|
9
|
1.66666666666667
|
14 |
GOURL
|
Gourmet Lanchonetes
|
Brazil
|
9
|
1.66666666666667
|
15 |
GOURL
|
Gourmet Lanchonetes
|
Brazil
|
9
|
1.66666666666667
|
16 |
QUEDE
|
Que Delícia
|
Brazil
|
9
|
1.66666666666667
|
17 |
QUEDE
|
Que Delícia
|
Brazil
|
9
|
1.66666666666667
|
18 |
QUEDE
|
Que Delícia
|
Brazil
|
9
|
1.66666666666667
|
19 |
WELLI
|
Wellington Importadora
|
Brazil
|
9
|
1.66666666666667
|
20 |
WELLI
|
Wellington Importadora
|
Brazil
|
9
|
1.66666666666667
|
21 |
WELLI
|
Wellington Importadora
|
Brazil
|
9
|
1.66666666666667
|
22 |
RICAR
|
Ricardo Adocicados
|
Brazil
|
11
|
1.77777777777778
|
23 |
QUEEN
|
Queen Cozinha
|
Brazil
|
13
|
1.88888888888889
|
24 |
HANAR
|
Hanari Carnes
|
Brazil
|
14
|
2
|
25 |
LAUGB
|
Laughing Bacchus Wine Cellars
|
Canada
|
3
|
1.33333333333333
|
26 |
MEREP
|
Mère Paillarde
|
Canada
|
13
|
1.66666666666667
|
27 |
BOTTM
|
Bottom-Dollar Markets
|
Canada
|
14
|
2
|
28 |
SIMOB
|
Simons bistro
|
Denmark
|
7
|
1.5
|
29 |
VAFFE
|
Vaffeljernet
|
Denmark
|
11
|
2
|
30 |
WILMK
|
Wilman Kala
|
Finland
|
7
|
1.5
|
31 |
WARTH
|
Wartian Herkku
|
Finland
|
15
|
2
|
32 |
PARIS
|
Paris spécialités
|
France
|
0
|
1.09090909090909
|
33 |
FRANR
|
France restauration
|
France
|
3
|
1.18181818181818
|
34 |
DUMON
|
Du monde entier
|
France
|
4
|
1.45454545454545
|
35 |
DUMON
|
Du monde entier
|
France
|
4
|
1.45454545454545
|
36 |
DUMON
|
Du monde entier
|
France
|
4
|
1.45454545454545
|
37 |
LACOR
|
La corne d'abondance
|
France
|
4
|
1.45454545454545
|
38 |
LACOR
|
La corne d'abondance
|
France
|
4
|
1.45454545454545
|
39 |
LACOR
|
La corne d'abondance
|
France
|
4
|
1.45454545454545
|
40 |
SPECD
|
Spécialités du monde
|
France
|
4
|
1.45454545454545
|
41 |
SPECD
|
Spécialités du monde
|
France
|
4
|
1.45454545454545
|
42 |
SPECD
|
Spécialités du monde
|
France
|
4
|
1.45454545454545
|
43 |
VINET
|
Vins et alcools Chevalier
|
France
|
5
|
1.63636363636364
|
44 |
VINET
|
Vins et alcools Chevalier
|
France
|
5
|
1.63636363636364
|
45 |
FOLIG
|
Folies gourmandes
|
France
|
5
|
1.63636363636364
|
46 |
FOLIG
|
Folies gourmandes
|
France
|
5
|
1.63636363636364
|
47 |
VICTE
|
Victuailles en stock
|
France
|
10
|
1.72727272727273
|
48 |
BLONP
|
Blondesddsl père et fils
|
France
|
11
|
1.81818181818182
|
49 |
LAMAI
|
La maison d'Asie
|
France
|
14
|
1.90909090909091
|
50 |
BONAP
|
Bon app'
|
France
|
17
|
2
|
51 |
MORGK
|
Morgenstern Gesundkost
|
Germany
|
5
|
1.09090909090909
|
52 |
ALFKI
|
Alfreds Futterkiste
|
Germany
|
6
|
1.36363636363636
|
53 |
ALFKI
|
Alfreds Futterkiste
|
Germany
|
6
|
1.36363636363636
|
54 |
ALFKI
|
Alfreds Futterkiste
|
Germany
|
6
|
1.36363636363636
|
55 |
DRACD
|
Drachenblut Delikatessen
|
Germany
|
6
|
1.36363636363636
|
56 |
DRACD
|
Drachenblut Delikatessen
|
Germany
|
6
|
1.36363636363636
|
57 |
DRACD
|
Drachenblut Delikatessen
|
Germany
|
6
|
1.36363636363636
|
58 |
TOMSP
|
Toms Spezialitäten
|
Germany
|
6
|
1.36363636363636
|
59 |
TOMSP
|
Toms Spezialitäten
|
Germany
|
6
|
1.36363636363636
|
60 |
TOMSP
|
Toms Spezialitäten
|
Germany
|
6
|
1.36363636363636
|
61 |
BLAUS
|
Blauer See Delikatessen
|
Germany
|
7
|
1.45454545454545
|
62 |
OTTIK
|
Ottilies Käseladen
|
Germany
|
10
|
1.63636363636364
|
63 |
OTTIK
|
Ottilies Käseladen
|
Germany
|
10
|
1.63636363636364
|
64 |
WANDK
|
Die Wandernde Kuh
|
Germany
|
10
|
1.63636363636364
|
65 |
WANDK
|
Die Wandernde Kuh
|
Germany
|
10
|
1.63636363636364
|
66 |
KOENE
|
Königlich Essen
|
Germany
|
14
|
1.72727272727273
|
67 |
FRANK
|
Frankenversand
|
Germany
|
15
|
1.90909090909091
|
68 |
FRANK
|
Frankenversand
|
Germany
|
15
|
1.90909090909091
|
69 |
LEHMS
|
Lehmanns Marktstand
|
Germany
|
15
|
1.90909090909091
|
70 |
LEHMS
|
Lehmanns Marktstand
|
Germany
|
15
|
1.90909090909091
|
71 |
QUICK
|
QUICK-Stop
|
Germany
|
28
|
2
|
72 |
HUNGO
|
Hungry Owl All-Night Grocers
|
Ireland
|
19
|
2
|
73 |
FRANS
|
Franchi S.p.A.
|
Italy
|
6
|
1.33333333333333
|
74 |
MAGAA
|
Magazzini Alimentari Riuniti
|
Italy
|
10
|
1.66666666666667
|
75 |
REGGC
|
Reggiani Caseifici
|
Italy
|
12
|
2
|
76 |
CENTC
|
Centro comercial Moctezuma
|
Mexico
|
1
|
1.2
|
77 |
ANATR
|
Ana Trujillo Emparedados y helados
|
Mexico
|
4
|
1.4
|
78 |
PERIC
|
Pericles Comidas clásicas
|
Mexico
|
6
|
1.6
|
79 |
ANTON
|
Antonio Moreno Taquería
|
Mexico
|
7
|
1.8
|
80 |
TORTU
|
Tortuga Restaurante
|
Mexico
|
10
|
2
|
81 |
SANTG
|
Santé Gourmet
|
Norway
|
6
|
2
|
82 |
WOLZA
|
Wolski Zajazd
|
Poland
|
7
|
2
|
83 |
PRINI
|
Princesa Isabel Vinhos
|
Portugal
|
5
|
1.5
|
84 |
FURIB
|
Furia Bacalhau e Frutos do Mar
|
Portugal
|
8
|
2
|
85 |
FISSA
|
FISSA Fabrica Inter. Salchichas S.A.
|
Spain
|
0
|
1.2
|
86 |
BOLID
|
Bólido Comidas preparadas
|
Spain
|
3
|
1.4
|
87 |
GALED
|
Galería del gastrónomo
|
Spain
|
5
|
1.8
|
88 |
GALED
|
Galería del gastrónomo
|
Spain
|
5
|
1.8
|
89 |
ROMEY
|
Romero y tomillo
|
Spain
|
5
|
1.8
|
90 |
ROMEY
|
Romero y tomillo
|
Spain
|
5
|
1.8
|
91 |
GODOS
|
Godos Cocina Típica
|
Spain
|
10
|
2
|
92 |
BERGS
|
Berglunds snabbköp
|
Sweden
|
18
|
1.5
|
93 |
FOLKO
|
Folk och fä HB
|
Sweden
|
19
|
2
|
94 |
CHOPS
|
Chop-suey Chinese
|
Switzerland
|
8
|
1.5
|
95 |
RICSU
|
Richter Supermarkt
|
Switzerland
|
10
|
2
|
96 |
CONSH
|
Consolidated Holdings
|
UK
|
3
|
1.28571428571429
|
97 |
CONSH
|
Consolidated Holdings
|
UK
|
3
|
1.28571428571429
|
98 |
NORTS
|
North/South
|
UK
|
3
|
1.28571428571429
|
99 |
NORTS
|
North/South
|
UK
|
3
|
1.28571428571429
|
100 |
EASTC
|
Eastern Connection
|
UK
|
8
|
1.42857142857143
|
101 |
SEVES
|
Seven Seas Imports
|
UK
|
9
|
1.57142857142857
|
102 |
BSBEV
|
B's Beverages
|
UK
|
10
|
1.85714285714286
|
103 |
BSBEV
|
B's Beverages
|
UK
|
10
|
1.85714285714286
|
104 |
ISLAT
|
Island Trading
|
UK
|
10
|
1.85714285714286
|
105 |
ISLAT
|
Island Trading
|
UK
|
10
|
1.85714285714286
|
106 |
AROUT
|
Around the Horn
|
UK
|
13
|
2
|
107 |
LAZYK
|
Lazy K Kountry Store
|
USA
|
2
|
1.07692307692308
|
108 |
TRAIH
|
Trail's Head Gourmet Provisioners
|
USA
|
3
|
1.23076923076923
|
109 |
TRAIH
|
Trail's Head Gourmet Provisioners
|
USA
|
3
|
1.23076923076923
|
110 |
THECR
|
The Cracker Box
|
USA
|
3
|
1.23076923076923
|
111 |
THECR
|
The Cracker Box
|
USA
|
3
|
1.23076923076923
|
112 |
THEBI
|
The Big Cheese
|
USA
|
4
|
1.38461538461538
|
113 |
THEBI
|
The Big Cheese
|
USA
|
4
|
1.38461538461538
|
114 |
LETSS
|
Let's Stop N Shop
|
USA
|
4
|
1.38461538461538
|
115 |
LETSS
|
Let's Stop N Shop
|
USA
|
4
|
1.38461538461538
|
116 |
HUNGC
|
Hungry Coyote Import Store
|
USA
|
5
|
1.46153846153846
|
117 |
LONEP
|
Lonesome Pine Restaurant
|
USA
|
8
|
1.53846153846154
|
118 |
SPLIR
|
Split Rail Beer & Ale
|
USA
|
9
|
1.61538461538462
|
119 |
OLDWO
|
Old World Delicatessen
|
USA
|
10
|
1.69230769230769
|
120 |
GREAL
|
Great Lakes Food Market
|
USA
|
11
|
1.76923076923077
|
121 |
WHITC
|
White Clover Markets
|
USA
|
14
|
1.84615384615385
|
122 |
RATTC
|
Rattlesnake Canyon Grocery
|
USA
|
18
|
1.92307692307692
|
123 |
SAVEA
|
Save-a-lot Markets
|
USA
|
31
|
2
|
124 |
GROSR
|
GROSELLA-Restaurante
|
Venezuela
|
2
|
1.25
|
125 |
LINOD
|
LINO-Delicateses
|
Venezuela
|
12
|
1.5
|
126 |
LILAS
|
LILA-Supermercado
|
Venezuela
|
14
|
1.75
|
127 |
HILAA
|
HILARION-Abastos
|
Venezuela
|
18
|
2
|