Window functions in Spark
================================================-> Spark Window functions operate on a group of rows like partition and return a single value for every input row. Spark SQL supports three kinds of window functions:
a) Ranking functions
b) Analytic functions
c) Aggregate functions
Ranking Functions:
=============
-> ROW_NUMBER(): It is used to get a unique sequential number for each row in the specified data.
-> RANK(): It is used to provide a rank to the result within a window partition. This function leaves gaps in rank when there are ties.
-> DENSE_RANK(): is used to get the result with rank of rows within a window partition without any gaps. This is similar to rank() function difference being rank function leaves gaps in rank when there are ties.
-> NTILE(): It is used to distribute the number of rows in the specified (N) number of groups. Each row group gets its rank as per the specified condition. We need to specify the value for the desired number of groups.
✔️Without use of partition by :
The NTILE(2) shows that we require a group of two records in the result.
✔️With the use of Partition by:
The NTILE(2), each partition in department group is divided into two groups.
✔️ Code Snippet:
๐ท๐ข๐ญ ๐ฅ๐ง = ๐๐ฆ๐ฒ((101,"๐๐ฐ๐ฉ๐ข๐ฏ","๐๐ฅ๐ฎ๐ช๐ฏ",4000),
(102, "๐๐ข๐ซ๐ฌ๐ถ๐ฎ๐ข๐ณ", "๐๐", 5000),
(103, "๐๐ฌ๐ฃ๐ข๐ณ", "๐๐",9990),
(104, "๐๐ฐ๐ณ๐ท๐ช๐ฏ", "๐๐ช๐ฏ๐ข๐ฏ๐ค๐ฆ", 7000),
(105, "๐๐ฐ๐ฉ๐ช๐ต", "๐๐", 3000),
(106, "๐๐ข๐ซ๐ฆ๐ด๐ฉ", "๐๐ช๐ฏ๐ข๐ฏ๐ค๐ฆ",9800),
(107, "๐๐ณ๐ฆ๐ฆ๐ต", "๐๐", 7000),
(108, "๐๐ข๐ณ๐บ๐ข๐ฎ", "๐๐ฅ๐ฎ๐ช๐ฏ",8000),
(109, "๐๐ข๐ฏ๐ซ๐ข๐บ", "๐๐", 7000),
(110, "๐๐ข๐ด๐ถ๐ฅ๐ฉ๐ข", "๐๐", 7000),
(111, "๐๐ฆ๐ญ๐ช๐ฏ๐ฅ๐ข", "๐๐", 8000),
(112, "๐๐ฐ๐ฎ๐ข๐ญ", "๐๐", 10000))
๐ช๐ฎ๐ฑ๐ฐ๐ณ๐ต ๐ด๐ฑ๐ข๐ณ๐ฌ.๐ช๐ฎ๐ฑ๐ญ๐ช๐ค๐ช๐ต๐ด._
๐ท๐ข๐ญ ๐ฅ๐ง2 = ๐ฅ๐ง.๐ต๐ฐ๐๐("๐ช๐ฅ","๐๐ข๐ฎ๐ฆ","๐๐ฆ๐ฑ๐ข๐ณ๐ต๐ฎ๐ฆ๐ฏ๐ต","๐๐ข๐ญ๐ข๐ณ๐บ")
๐ท๐ข๐ญ ๐ธ๐ช๐ฏ๐ฅ๐ฐ๐ธ = ๐๐ช๐ฏ๐ฅ๐ฐ๐ธ.๐ฑ๐ข๐ณ๐ต๐ช๐ต๐ช๐ฐ๐ฏ๐๐บ("๐๐ฆ๐ฑ๐ข๐ณ๐ต๐ฎ๐ฆ๐ฏ๐ต").๐ฐ๐ณ๐ฅ๐ฆ๐ณ๐๐บ("๐๐ข๐ญ๐ข๐ณ๐บ")
๐ฅ๐ง2.๐ธ๐ช๐ต๐ฉ๐๐ฐ๐ญ๐ถ๐ฎ๐ฏ("๐ณ๐ฐ๐ธ_๐ฏ๐ถ๐ฎ๐ฃ๐ฆ๐ณ",๐ณ๐ฐ๐ธ_๐ฏ๐ถ๐ฎ๐ฃ๐ฆ๐ณ.๐ฐ๐ท๐ฆ๐ณ(๐ธ๐ช๐ฏ๐ฅ๐ฐ๐ธ))
.๐ธ๐ช๐ต๐ฉ๐๐ฐ๐ญ๐ถ๐ฎ๐ฏ("๐ณ๐ข๐ฏ๐ฌ",๐ณ๐ข๐ฏ๐ฌ().๐ฐ๐ท๐ฆ๐ณ(๐ธ๐ช๐ฏ๐ฅ๐ฐ๐ธ))
.๐ธ๐ช๐ต๐ฉ๐๐ฐ๐ญ๐ถ๐ฎ๐ฏ("๐ฅ๐ฆ๐ฏ๐ด๐ฆ_๐ณ๐ข๐ฏ๐ฌ",๐ฅ๐ฆ๐ฏ๐ด๐ฆ_๐ณ๐ข๐ฏ๐ฌ().๐ฐ๐ท๐ฆ๐ณ(๐ธ๐ช๐ฏ๐ฅ๐ฐ๐ธ))
.๐ธ๐ช๐ต๐ฉ๐๐ฐ๐ญ๐ถ๐ฎ๐ฏ("๐ฏ๐ต๐ช๐ญ๐ฆ",๐ฏ๐ต๐ช๐ญ๐ฆ(2).๐ฐ๐ท๐ฆ๐ณ(๐ธ๐ช๐ฏ๐ฅ๐ฐ๐ธ))
.๐ด๐ฉ๐ฐ๐ธ()