Skip to main content
PYSPARK โ€ข LESSON 78

Multi-Metric Aggregate Calculations

How do we compute order count, gross sales, and average ticket size simultaneously in one pass?

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

How do we compute order count, gross sales, and average ticket size simultaneously in one pass?

๐Ÿ’ก WHAT IS IT?

Passing multiple aggregate expressions into select() to perform comprehensive statistical profiling.

๐ŸŽฏ WHAT IS IT USED FOR?

Daily KPI snapshots, executive summary cards, and business health monitoring.

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

df.select(
    count(col("order_id")).alias("order_count"),
    sum(col("amount")).alias("gross_sales"),
    avg(col("amount")).alias("avg_ticket_size")
).show()

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Multi-Metric Aggregate Calculations.

  • โœ“ Compute count, sum, and avg simultaneously
  • โœ“ Alias all calculated metrics
  • โœ“ Display executive business summary