T-SQL - RANKING Windowing Functions for Beginners

as usual: english part at the end

Hi,

Der Grund dieses Blog-Eintrages ist schlicht der, dass ich mit dem Grundverständnis der verschienen "Window RANKING" Funktionen im SQL Server so meine Probleme hatte, da ich die diversen Erklärungen nicht gleich verstanden habe. Und ich dachte mir, vielleicht geht es auch Anderen so ...

RANKING Windowing Functionen

- DENSE_RANK
- RANK
- ROW_NUMBER
- NTILE

Alle mit der OVER Klausel

es gibt noch weitere Windowing Functions mit der OVER Klausel, diese sind jedoch mehrheitlich eher statistischer Natur.

OVER-Klausel
- ORDER BY bestimmt die Reihenfolge der erzeugten Nummern

- PARTITION BY - Optional - definiert die Anzahl der verwendeten Sets / Partitions.
wenn PARTITION BY angegeben wird, startet die Reihenfolge bei jeder neuen Partition wieder bei 1
wenn kein PARTITION BY angegeben wird, werden alle ausgewählten Datensätze als ein einziges Set / Partition betrachtet.

- - - - - - - -

- ROW_NUMBER
Wie der Name besagt, Zeilennummern - ORDER BY bestimmt die Reihenfolge, wenn PARTITION BY,
dann startet die Nr bei einer neuen Partition wieder bei 1

- DENSE_RANK
Erzeugt pro Partition und Sortierung eine eigene - fortlaufende - Nummer ohne Lücken in der Numerierung

- RANK
Erzeugt pro Partition und Sortierung eine eigene - fortlaufende - Nummer mit Lücken in der Numerierung
wobei die Lückengröße immer der Anzahl der gleichen Daten entspricht. 
Geeignet für die Erzeugung von Siegplätzen bei Wettkämpfen, wenn es zwei 1. Plätze gibt, ist das Ranking
1 1 3 und es gibt somit keinen 2. Platz

 

- NTILE
teilt eine Tabelle / Abfrage in exakt n gleichgroße Stücke (plus Rest)

NTILE is wichtig zum Laden von "Junks" von Datensätzen
(z.B. kann man bei PowerApps max. 500 Datensätze auf einmal downloaden / bearbeiten)

Wenn also nicht die Anzahl der Teilstücke interessieren, sondern nur die max. Anzahl pro Datensätze pro Teilstück:
Einfach die Gesamtanzahl der Datensätze durch die gewünschte Anzahl dividieren und diesen Wert als @ntile_value verwenden ...

Wenn man ein bestehendes Set von ungefähr dieser Größe hat, z.b. alle Tage eines Jahres, dann ist Dense_Rank besser geeignet
(da in diesem Fall die unterschiedliche Set-Größe wg. eines Schaltjahres automatisch berücksichtigt wird)

Beispiel:

Für dieses Beispiel würde eine Tabelle [_tblAlleTage] benötigt,
aber das Beispiel sollte auch so eingängig sein.

extrem stark gekürzt

Tabelle [_tblAlleTage]

JJJJMMTT dtDatum JahrNr Quartal MonatNr TagNr Wochentag ID
19900101 1990-01-01 00:00:00.000 1990 1 1 1 1 0
19900102 1990-01-02 00:00:00.000 1990 1 1 2 2 1
19900103 1990-01-03 00:00:00.000 1990 1 1 3 3 2
19900104 1990-01-04 00:00:00.000 1990 1 1 4 4 3
19900105 1990-01-05 00:00:00.000 1990 1 1 5 5 4
19900106 1990-01-06 00:00:00.000 1990 1 1 6 6 5
19900107 1990-01-07 00:00:00.000 1990 1 1 7 7 6
19900108 1990-01-08 00:00:00.000 1990 1 1 8 1 7
19900109 1990-01-09 00:00:00.000 1990 1 1 9 2 8
19900110 1990-01-10 00:00:00.000 1990 1 1 10 3 9
....

use nuetzliche_Tabellen
go

Declare @ntile_value as int

select @ntile_value = Count( Distinct JahrNr) from dbo.[_tblAlleTage]
print @ntile_value -- 110

--select @ntile_value = Count(*) from dbo.[_tblAlleTage]
--set @ntile_value = @ntile_value / 480

SELECT *
FROM
(
SELECT JJJJMMTT,
[ID],
jahrnr,
NTILE(@ntile_value) OVER(ORDER BY [JJJJMMTT]) AS NTILE_X,
DENSE_RANK() OVER(ORDER BY JahrNr) AS Dense_Rank_X,
RANK() OVER(ORDER BY JahrNr) AS Rank_X,
ROW_NUMBER() OVER(ORDER BY JJJJMMTT) AS Row_Number_X,
ROW_NUMBER() OVER(PARTITION BY JahrNr ORDER BY JJJJMMTT) AS Tag_desJahres_X
FROM dbo.[_tblAlleTage]
) a
WHERE ....

Resultset:

JJJJMMTT ID jahrnr NTILE_X Dense_Rank_X Rank_X Row_Number_X Tag_desJahres_X
19900101 0 1990 1 1 1 1 1
19900102 1 1990 1 1 1 2 2
19900103 2 1990 1 1 1 3 3
19900104 3 1990 1 1 1 4 4
19900105 4 1990 1 1 1 5 5
....
19901230 363 1990 1 1 1 364 364
19901231 364 1990 1 1 1 365 365
19910101 365 1991 1 2 366 366 1
19910102 366 1991 2 2 366 367 2
19910103 367 1991 2 2 366 368 3
19910104 368 1991 2 2 366 369 4

Man kann auch Row_Numbers pro NTILE vergeben, ist aber etwas "tricky":

DECLARE @ntile_value AS INT;

select @ntile_value = Count(*) from dbo.[_tblAlleTage]
set @ntile_value = @ntile_value / 480

SELECT * FROM
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY a.[NTILE_X] ORDER BY a.Row_Number_X) AS NTILE_ROW_X

FROM
(
SELECT JJJJMMTT,
[ID],
jahrnr,
DENSE_RANK() OVER(ORDER BY JahrNr) AS Dense_Rank_X,
RANK() OVER(ORDER BY JahrNr) AS Rank_X,
ROW_NUMBER() OVER(ORDER BY JJJJMMTT) AS Row_Number_X,
NTILE(@ntile_value) OVER(ORDER BY [JJJJMMTT]) AS NTILE_X
FROM dbo.[_tblAlleTage]
) a
) b
WHERE ...

------------------------------------------ english part ----------------------------------


The reason of this blog entry is simply that I had my problems with the basic understanding of the different "Window RANKING" functions in the SQL Server, because I did not understand the required explanations immediately. And I thought to myself, maybe others are doing the same ...

RANKING windowing functions

- DENSE_RANK
- RANK
- ROW_NUMBER
- NTILE

All with the OVER clause

There are other windowing functions with the OVER clause, but these are mostly statistical in nature.

OVER clause
- ORDER BY determines the order of the generated numbers

- PARTITION BY - Optional - defines the number of used sets / partitions.
if PARTITION BY is specified, the order will start at 1 for each new partition,
if no PARTITION BY is specified, all selected records will be considered as a single set / partition.

- - - - - - - -

- ROW_NUMBER
As the name implies, row numbers - ORDER BY determines the order
when PARTITION BY, then the number restarts again at 1 at each new partition

- DENSE_RANK
Creates a separate - consecutive - number without gaps for each partition and sorting

- RANK
Creates a separate - consecutive - number with gaps for each partition and sorting
where the gap size always equals the number of the same data.
Suitable for generating victory places in competitions, if there are two 1st places, the ranking is
1 1 3 and there is no 2nd place

- NTILE
divide a table / query into exactly n equal pieces (plus remainder)

Important if loading a complete "junk" of records
(For example, with PowerApps, you can download / edit a maximum of 500 records at once)

So if not the number of cuts interested, but only the max. Number per data set per section:
Simply divide the total number of records by the desired number and use that value as @ntile_value ...

If one has an existing set of about this size, e.g. every day of the year, then Dense_Rank is better suited
(since it automatically takes leap years into account - in this sample).

Example:

For this example, a table would be needed [_tblAlleTage]
but the example should also be so catchy.

extremely shortened

Tabelle [_tblAlleTage]

JJJJMMTT dtDatum JahrNr Quartal MonatNr TagNr Wochentag ID
19900101 1990-01-01 00:00:00.000 1990 1 1 1 1 0
19900102 1990-01-02 00:00:00.000 1990 1 1 2 2 1
19900103 1990-01-03 00:00:00.000 1990 1 1 3 3 2
19900104 1990-01-04 00:00:00.000 1990 1 1 4 4 3
19900105 1990-01-05 00:00:00.000 1990 1 1 5 5 4
19900106 1990-01-06 00:00:00.000 1990 1 1 6 6 5
19900107 1990-01-07 00:00:00.000 1990 1 1 7 7 6
19900108 1990-01-08 00:00:00.000 1990 1 1 8 1 7
19900109 1990-01-09 00:00:00.000 1990 1 1 9 2 8
19900110 1990-01-10 00:00:00.000 1990 1 1 10 3 9
....

use nuetzliche_Tabellen
go

Declare @ntile_value as int

select @ntile_value = Count( Distinct JahrNr) from dbo.[_tblAlleTage]
print @ntile_value -- 110

--select @ntile_value = Count(*) from dbo.[_tblAlleTage]
--set @ntile_value = @ntile_value / 480

SELECT *
FROM
(
SELECT JJJJMMTT,
[ID],
jahrnr,
NTILE(@ntile_value) OVER(ORDER BY [JJJJMMTT]) AS NTILE_X,
DENSE_RANK() OVER(ORDER BY JahrNr) AS Dense_Rank_X,
RANK() OVER(ORDER BY JahrNr) AS Rank_X,
ROW_NUMBER() OVER(ORDER BY JJJJMMTT) AS Row_Number_X,
ROW_NUMBER() OVER(PARTITION BY JahrNr ORDER BY JJJJMMTT) AS day_of_year_X
FROM dbo.[_tblAlleTage]
) a
WHERE ....

Resultset:

JJJJMMTT ID jahrnr NTILE_X Dense_Rank_X Rank_X Row_Number_X day_of_year_X
19900101 0 1990 1 1 1 1 1
19900102 1 1990 1 1 1 2 2
19900103 2 1990 1 1 1 3 3
19900104 3 1990 1 1 1 4 4
19900105 4 1990 1 1 1 5 5
....
19901230 363 1990 1 1 1 364 364
19901231 364 1990 1 1 1 365 365
19910101 365 1991 1 2 366 366 1
19910102 366 1991 2 2 366 367 2
19910103 367 1991 2 2 366 368 3
19910104 368 1991 2 2 366 369 4

You can also assign Row_Numbers per NTILE, but it's a bit tricky:

DECLARE @ntile_value AS INT;

select @ntile_value = Count(*) from dbo.[_tblAlleTage]
set @ntile_value = @ntile_value / 480

SELECT * FROM
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY a.[NTILE_X] ORDER BY a.Row_Number_X) AS NTILE_ROW_X

FROM
(
SELECT JJJJMMTT,
[ID],
jahrnr,
DENSE_RANK() OVER(ORDER BY JahrNr) AS Dense_Rank_X,
RANK() OVER(ORDER BY JahrNr) AS Rank_X,
ROW_NUMBER() OVER(ORDER BY JJJJMMTT) AS Row_Number_X,
NTILE(@ntile_value) OVER(ORDER BY [JJJJMMTT]) AS NTILE_X
FROM dbo.[_tblAlleTage]
) a
) b
WHERE ...

mfg Klaus