Skip to main content
PYSPARK โ€ข LESSON 83

Segment Totals with sum()

How do we calculate total sales revenue by geographic region using agg() and sum()?

Intermediateโฑ 2 Minutesโญ 400 XP
๐Ÿค” THE QUESTION

How do we calculate total sales revenue by geographic region using agg() and sum()?

๐Ÿ’ก WHAT IS IT?

groupBy('region').agg(sum('sales')) groups by territory and computes the total monetary sales.

๐ŸŽฏ WHAT IS IT USED FOR?

Regional performance leaderboards, sales compensation tracking, and regional quota monitoring.

๐Ÿ’ป EXAMPLE
from pyspark.sql.functions import col, sum

df.groupBy("region").agg(sum(col("sales")).alias("total_sales")).show()

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Segment Totals with sum().

  • โœ“ Group by geographic region
  • โœ“ Sum regional sales totals
  • โœ“ Alias output total sales column