liveBook · Manning

Data aggregation

As explained in Digital Twins in Action

An aggregate is a summary value computed from multiple data records, such as an average, minimum, or maximum.

Data aggregation computes summary values from multiple records. For time-series data, it commonly groups readings into time windows and calculates statistics such as the mean, minimum, maximum, or standard deviation. The result is a smaller representation suited to dashboards and historical analysis. section 6.3.2 section 4.3.3

Daily temperature summaries

A home sensor produces 6,375 readings over 45 days. Calculating the daily low, high, and mean reduces these to 45 summary rows. Plotting the summaries makes the upward spring trend and unusual patterns easier to see than inspecting the raw readings. section 6.2.1

timestamp,min,max,mean
1754784000,13.8,18.4,16.31
1754870400,12.8,17.6,15.57
1754956800,13.3,17.4,15.99
1755043200,15.8,19.3,18.14
.
. 40 readings
.
1758585600,19.9,22.7,21.64

The summary rows retain a timestamp and three statistics for each day, rather than a separate row for every sensor reading. section 6.2.1

Aggregation differs from decimation. Decimation removes points while preserving the essential shape of a curve; aggregation computes statistics over time buckets. Aggregation suits long-term trends and multiresolution dashboards, while decimation suits zoomed-in views of high-frequency data. section 6.3.2

Choose the aggregation strategy for the job:

  • Precompute summaries at minute, hour, day, or month resolutions. A dashboard can switch between these grains as the user zooms or the viewport changes, avoiding repeated processing of raw readings. section 6.3.2 section 4.3.3
  • Maintain a sliding-window aggregate for a live stream. For a 30-second temperature average, add new readings, drop old ones, and recalculate the average to smooth noise without waiting for a batch. section 4.4.2
  • Aggregate during ETL when derived data is needed repeatedly. For example, Apache Spark can group minute-by-minute power records into weekly consumption and cost values for a dashboard. section 4.4.4

The choice of statistic and time window determines what the summary reveals. A mean highlights an overall trend, while minimum and maximum values retain the extremes within each interval. For trustworthy analytical results, record the transformations and parameters used to derive aggregated datasets. section 6.2.1 section 4.4.4

Listing 4.9 An example of using Apache Spark to extract minute-by-minute power
from pyspark.sql import SparkSession
from pyspark.sql.functions import date_trunc, sum as spark_sum, avg, col
from pyspark.sql.types import *

spark = SparkSession.builder.appName("WeeklyUsage").getOrCreate()

schema = StructType([  #1
    StructField("datetime_utc", TimestampType(), True),
    StructField("datetime_local", TimestampType(), True),
    StructField("watt_hours", DoubleType(), True),
    StructField("cost_dollars", DoubleType(), True),
    StructField("is_peak", BooleanType(), True)
])

df = spark.read.option("header", "true").schema(schema).csv(
  "powerpal_data.csv")

weekly_usage = df.groupBy(  #2
    date_trunc("week", col("datetime_local")).alias("week_start")
).agg(
    (spark_sum("watt_hours") / 1000).alias("total_kwh"),
    spark_sum("cost_dollars").alias("total_cost_dollars")
).orderBy("week_start")

print("Weekly Usage (kWh) and Cost:")
weekly_usage.show()

spark.stop()

The Spark example groups power readings by local week and sums energy and cost for dashboard use. section 4.4.4

How Digital Twins in Action covers it

How deep it goes
Explains time-window statistics, multiple resolutions, sliding windows, and an ETL grouping example, without detailing aggregation internals.
Examples
Uses daily home-temperature summaries, a 30-second sliding average, and weekly power consumption and cost.
What you can do after
Evaluate/understand when to use time-window summaries instead of decimation and how to select a resolution for a dashboard.
How it connects
Connects aggregation to time-series databases, data visualization, decimation, streaming ingestion, and ETL.

Also explained in

Compare how 19 books explain data aggregation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Digital Twins in Action. The section links point to the parts of the book it draws on.