title: How to avoid multiple window functions in a expression in pyspark
body: <p>I want spark to avoid creating two separate window stage, for same window object used twice in my code.</p>
  
  <p>How can I use it once in my code in the following example, and tell spark to do sum and division under single window.</p>
  
  <pre class="lang-py prettyprint-override"><code>df = df.withColumn("colum_c", 
              f.sum(f.col("colum_a")).over(window) /
              f.sum(f.col("colum_b")).over(window))
  </code></pre>
  
  <p>Example:</p>
  
  <pre class="lang-py prettyprint-override"><code>days = lambda i: (i - 1) * 86400
  
  window = (
      Window()
      .partitionBy(f.col("account_id"))
      .orderBy(f.col("event_date").cast("timestamp").cast("long"))
      .rangeBetween(-days(7), 0)
  )
  
  df.withColumn(
      "fea_fuel_consumption_ratio_petrol_diesel_01w",
      (
          f.sum(f.col("fea_fuel_consumption_petrol")).over(window)
          / f.sum(f.col("fea_fuel_consumption_diesel")).over(window)
      ),
  ).show(1000, False)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/FgnEf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FgnEf.png" alt="Spark Created Two different window"></a></p>