Created
March 25, 2023 17:32
-
-
Save subhsaha/a0ac9ba95909f982406fbb0ed0a41a67 to your computer and use it in GitHub Desktop.
AVG window function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.spark.sql.expressions.Window | |
import org.apache.spark.sql.functions._ | |
val winsales = spark.table("winsales") | |
val windowSpec = Window.orderBy(col("dateid"), col("salesid")).rowsBetween(Window.unboundedPreceding, Window.currentRow) | |
val result = winsales | |
.select(col("salesid"), col("dateid"), col("sellerid"), col("qty"), | |
avg(col("qty")).over(windowSpec).as("avg")) | |
.orderBy(col("dateid"), col("salesid")) | |
result.show() | |
//https://docs.aws.amazon.com/redshift/latest/dg/r_WF_AVG.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment