{
  "license": "CC BY 4.0",
  "licenseUrl": "https://creativecommons.org/licenses/by/4.0/",
  "recipe": {
    "slug": "sensor-deliveries-vs-measurements",
    "title": "Count sensor deliveries and measurements separately",
    "testedWith": "Apache DataFusion 45.2.0",
    "lastReviewed": "2026-09-24"
  },
  "contract": {
    "tableName": "sensor_deliveries",
    "schema": [
      {
        "name": "reading_id",
        "type": "Utf8",
        "description": "Identity of the fictional physical measurement."
      },
      {
        "name": "measured_second",
        "type": "Int64",
        "description": "Seconds after the exercise begins when the sensor measured."
      },
      {
        "name": "delivered_second",
        "type": "Int64",
        "description": "Seconds after the exercise begins when a network message arrived."
      },
      {
        "name": "co2_ppm",
        "type": "Int64",
        "description": "Invented CO₂ reading in parts per million; not a health assessment."
      }
    ]
  },
  "query": "WITH per_reading AS (\n  SELECT reading_id, MIN(co2_ppm) AS co2_ppm\n  FROM sensor_deliveries\n  GROUP BY reading_id\n),\ndelivery_summary AS (\n  SELECT COUNT(*) AS deliveries, AVG(co2_ppm) AS delivered_mean_ppm\n  FROM sensor_deliveries\n),\nmeasurement_summary AS (\n  SELECT COUNT(*) AS measurements, AVG(co2_ppm) AS measurement_mean_ppm\n  FROM per_reading\n)\nSELECT deliveries, measurements, delivered_mean_ppm, measurement_mean_ppm\nFROM delivery_summary CROSS JOIN measurement_summary;",
  "fixtureKind": "reproducible",
  "inputRows": [
    {
      "reading_id": "r1",
      "measured_second": 0,
      "delivered_second": 1,
      "co2_ppm": 600
    },
    {
      "reading_id": "r2",
      "measured_second": 10,
      "delivered_second": 11,
      "co2_ppm": 650
    },
    {
      "reading_id": "r3",
      "measured_second": 20,
      "delivered_second": 21,
      "co2_ppm": 700
    },
    {
      "reading_id": "r4",
      "measured_second": 30,
      "delivered_second": 31,
      "co2_ppm": 750
    },
    {
      "reading_id": "r4",
      "measured_second": 30,
      "delivered_second": 32,
      "co2_ppm": 750
    },
    {
      "reading_id": "r4",
      "measured_second": 30,
      "delivered_second": 33,
      "co2_ppm": 750
    }
  ],
  "illustrativeInputRows": [
    {
      "reading_id": "r1",
      "measured_second": 0,
      "delivered_second": 1,
      "co2_ppm": 600
    },
    {
      "reading_id": "r2",
      "measured_second": 10,
      "delivered_second": 11,
      "co2_ppm": 650
    },
    {
      "reading_id": "r3",
      "measured_second": 20,
      "delivered_second": 21,
      "co2_ppm": 700
    },
    {
      "reading_id": "r4",
      "measured_second": 30,
      "delivered_second": 31,
      "co2_ppm": 750
    },
    {
      "reading_id": "r4",
      "measured_second": 30,
      "delivered_second": 32,
      "co2_ppm": 750
    },
    {
      "reading_id": "r4",
      "measured_second": 30,
      "delivered_second": 33,
      "co2_ppm": 750
    }
  ],
  "expectedOutput": {
    "columns": [
      "deliveries",
      "measurements",
      "delivered_mean_ppm",
      "measurement_mean_ppm"
    ],
    "rows": [
      {
        "deliveries": 6,
        "measurements": 4,
        "delivered_mean_ppm": 700,
        "measurement_mean_ppm": 675
      }
    ]
  },
  "notes": [
    "The included synthetic input rows generate the expected output when timestamps are shifted into the documented query window.",
    "Run this fixture in the public browser SQL playground; the selected query executes locally with DuckDB-Wasm.",
    "Adapt time windows, thresholds, identities, and business definitions before operational use."
  ]
}