ML Engineer MasterClass (October) | 4 seats left

Spark · Aggregate functions
AmazonAmazon Analytics
Amazon · Aggregate and Analyze

Aggregate functions

Count order lines and distinct products, then summarize line values.

Step 1 of 6 · Learn

Count lines and products

agg produces a one-row summary. count("*") counts every row; count(column) skips nulls and countDistinct counts distinct non-null values. There are nine order lines, but products can appear on more than one line. None of these expressions sums quantity.

Lesson reference: PySpark and Scala

Count lines and products

agg produces a one-row summary. count("*") counts every row; count(column) skips nulls and countDistinct counts distinct non-null values. There are nine order lines, but products can appear on more than one line. None of these expressions sums quantity.

PySpark example

from pyspark.sql.functions import col, count, countDistinct, sum, avg, round

result = order_items.agg(count("*").alias("value_count"))
result.show()

Scala example

import org.apache.spark.sql.functions._

val result = order_items.agg(count("*").alias("value_count"))
result.show()

Sum line values

line_total is the value of every unit on one line, so sum(line_total) adds line values without multiplying by quantity again. The example includes every line. These are order values, not recognized revenue; the linked orders have different statuses.

PySpark example

from pyspark.sql.functions import col, count, countDistinct, sum, avg, round

result = order_items.agg(
    sum("line_total").alias("total_line_value")
)
result.show()

Scala example

import org.apache.spark.sql.functions._

val result = order_items.agg(
    sum("line_total").alias("total_line_value")
)
result.show()

Average line values

avg(line_total) computes an unweighted mean across lines, not an average unit price or order value. Round the final average to two decimals. Null values are skipped; all-null inputs yield null, not zero.

PySpark example

from pyspark.sql.functions import col, count, countDistinct, sum, avg, round

result = order_items.agg(
    round(avg("line_total"), 2).alias("average_line_value")
)
result.show()

Scala example

import org.apache.spark.sql.functions._

val result = order_items.agg(
    round(avg("line_total"), 2).alias("average_line_value")
)
result.show()
example.pyPySpark
1. Read order_itemsOne row per order line; quantity can exceed one.
2. Aggregate the selected linesCount, sum, or average only the rows passed into agg.
3. Inspect the summaryLine count, distinct products, line value, and unit quantity answer different questions.
Source order_items9 rows
order_item_idorder_idproduct_idquantityline_total
11001201159.5
21001202130
31002203299
41002206150
51003204135
610042033149.97
71004205170.02
81005203149.99
910062012120
Count lines and products
Result1 rows
value_count
9
Compare the source columns with the transformed result.

The example is loaded in the editor. Run it as written, then try a small change.

Runs on the Spark backend. First startup may take a moment.

Run your code to see the result.