This post is Regarding Row_Number,RANK and Denserank.
Row_Number:= Using this function one can retuen unique id to each row.
Row_Number:= Using this function one can retuen unique id to each row.
Syntex:
SELECT
Col_Value,
ROW_NUMBER() OVER (ORDER BY Col_Value) AS 'RowID'
FROM
Table;
Result:
Col RowID
A 1
A 2
A 3
B 4
B 5
C 6
C 7
RANK:-This function will return a unique number to each distinct row, but it leaves a gap between the groups.
Syntex:-
SELECT
Col_Value,
Rank() OVER (ORDER BY Col_Value) AS 'RowID'
FROM
Table;
Result:-
Col RowID
A 1
A 1
A 1
B 4
B 4
C 6
C 6
DENSE_RANK:-This function is similar to Rank with only difference, this will not leave gaps between groups.
it generates a unique id for each group and without repetition.
Syntex:-
SELECT
Col_Value,
DENSE_RANK() OVER (ORDER BY Col_Value) AS 'RowID'
FROM
Table;
Result:-
Col RowID
A 1
A 1
A 1
B 2
B 2
C 3
C 3
SELECT
Col_Value,
ROW_NUMBER() OVER (ORDER BY Col_Value) AS 'RowID'
FROM
Table;
Result:
Col RowID
A 1
A 2
A 3
B 4
B 5
C 6
C 7
RANK:-This function will return a unique number to each distinct row, but it leaves a gap between the groups.
Syntex:-
SELECT
Col_Value,
Rank() OVER (ORDER BY Col_Value) AS 'RowID'
FROM
Table;
Result:-
Col RowID
A 1
A 1
A 1
B 4
B 4
C 6
C 6
DENSE_RANK:-This function is similar to Rank with only difference, this will not leave gaps between groups.
it generates a unique id for each group and without repetition.
Syntex:-
SELECT
Col_Value,
DENSE_RANK() OVER (ORDER BY Col_Value) AS 'RowID'
FROM
Table;
Result:-
Col RowID
A 1
A 1
A 1
B 2
B 2
C 3
C 3
Comments
Post a Comment