Querying the lakehouse

The following queries demonstrate how to extract common metrics and insights from the lakehouse. They use the Dremio SQL dialect as an example. Other query engines sometimes need small changes, such as quoting reserved words or unnesting arrays. To keep the queries concise, they refer to tables by namespace and table name only, such as cdc_event.event. Depending on your query engine, add the name of the catalog as a prefix, or set the catalog as the default.

Basic inventory queries

To list all devices registered in your tenant, use the latest_inventory tables to get the most recent state and filter for the marker fragment c8y_IsDevice in the fragments array.

SELECT id, name
FROM latest_inventory.inventory
WHERE ARRAY_CONTAINS(fragments, 'c8y_IsDevice')

To find the current location of all devices within a specific geographic boundary, query the c8y_Position fragment in the latest_inventory folder.

SELECT id, lat, lng
FROM latest_inventory.c8y_Position
WHERE lat BETWEEN 50.0 AND 52.0
  AND lng BETWEEN 6.0 AND 8.0

Basic time series queries

To retrieve all events recorded for a specific device within a given timespan, query the cdc_event.event table. In Dremio SQL, time is a reserved word, so put it in double quotes.

SELECT "time", type, text
FROM cdc_event.event
WHERE source = '52277201'
  AND "time" BETWEEN '2026-04-16 00:00:00' AND '2026-04-16 23:59:59'
ORDER BY "time" DESC

Parent and child queries

To find a parent device or asset linked to a specific device, you can search for the child’s ID within the childDevices array.

SELECT id, name
FROM latest_inventory.inventory
WHERE ARRAY_CONTAINS(childDevices, '102938')

Conversely, to retrieve all child devices belonging to a specific asset, use Dremio’s FLATTEN function to unroll the asset’s childDevices array and select the matching devices.

SELECT id, name
FROM latest_inventory.inventory
WHERE id IN (
  SELECT FLATTEN(childDevices)
  FROM latest_inventory.inventory
  WHERE id = '102938'
)

Measurement queries

To view a specific measurement series for a device over time, query the corresponding measurement fragment table. The following example fetches the voltage and temperature values of the c8y_Battery fragment.

SELECT "time", b.voltage."value" AS voltage, b.temperature."value" AS temperature
FROM cdc_measurement.c8y_Battery b
WHERE source = '47635' AND
 "time" BETWEEN '2026-04-02 00:00:00' AND '2026-04-02 23:59:59'
ORDER BY "time" DESC

Dremio SQL requires a table name or table alias in front of structured properties. In the example, voltage."value" alone does not work. Use b.voltage."value" instead. To filter for values that cross a threshold, add a condition to the previous query:

SELECT "time", b.voltage."value" AS voltage, b.temperature."value" AS temperature
FROM cdc_measurement.c8y_Battery b
WHERE source = '47635' AND
 "time" BETWEEN '2026-04-02 00:00:00' AND '2026-04-02 23:59:59' AND
 b.voltage."value" < 11.5
ORDER BY "time" DESC

To create reports that are easier to read, you can join the historical measurement data together with the latest inventory data. This allows you to display the current device name alongside the recorded measurement values.

SELECT b."time", i.name, b.voltage."value" AS voltage
FROM cdc_measurement.c8y_Battery b JOIN latest_inventory.inventory i ON b.source = i.id
WHERE source = '47635' AND
 "time" BETWEEN '2026-04-02 00:00:00' AND '2026-04-02 23:59:59' AND
 b.voltage."value" < 11.5
ORDER BY "time" DESC

Ensuring good query performance

While the lakehouse offers the flexibility of arbitrary SQL querying, performance can vary depending on the complexity of your query. To ensure fast responses for common IoT use cases, performance is highly optimized for queries that select a specific set of devices, a particular time range, and, for measurements, a limited set of data points.

Other types of queries can be slower. These include queries that process a very large base set of data with complex aggregations or execute multi-level joins with selections on non-indexed properties. To improve query performance for your specific applications, you have several options:

  • Pre-process the data before it enters Cumulocity using Edge functionality or Data Preparation functions.
  • Process the data within Cumulocity using Streaming Analytics or a custom microservice to create refined data streams.
  • Post-process the data in the lakehouse by creating your own gold layer tables with pre-aggregated and materialized results, using external lakehouse tools.