Skip to content

Instantly share code, notes, and snippets.

@subhsaha
Created March 25, 2023 17:32
Show Gist options
  • Save subhsaha/a0ac9ba95909f982406fbb0ed0a41a67 to your computer and use it in GitHub Desktop.
Save subhsaha/a0ac9ba95909f982406fbb0ed0a41a67 to your computer and use it in GitHub Desktop.
AVG window function
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