Thursday, May 11, 2023

Spark- Window Function

 

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).๐˜ฐ๐˜ท๐˜ฆ๐˜ณ(๐˜ธ๐˜ช๐˜ฏ๐˜ฅ๐˜ฐ๐˜ธ))
 .๐˜ด๐˜ฉ๐˜ฐ๐˜ธ()


No comments:

Post a Comment

Spark- Window Function

  Window functions in Spark ================================================ -> Spark Window functions operate on a group of rows like pa...