title | sidebar_label | description |
---|---|---|
GROUP BY keyword | GROUP BY | GROUP BY SQL keyword reference documentation. |
Groups aggregation calculations by one or several keys. In QuestDB, this clause is optional.
Syntax
:::note
QuestDB groups aggregation results implicitly and does not require the GROUP BY keyword. It is only supported for convenience. Using the GROUP BY clause explicitly will return the same results as if the clause was omitted.
:::
Examples
The below queries perform aggregations on a single key. Using GROUP BY
explicitly or implicitly yields the same results:
SELECT sensorId, avg(temp)
FROM readings
GROUP BY sensorId;
SELECT sensorId, avg(temp)
FROM readings;
The below queries perform aggregations on multiple keys. Using GROUP BY
explicitly or implicitly yields the same results:
SELECT sensorId, sensorType, avg(temp)
FROM readings
GROUP BY sensorId,sensorType;
SELECT sensorId, sensorType, avg(temp)
FROM readings;
When used explicitly, the list of keys in the GROUP BY
clause must match the list of keys in the SELECT
clause, otherwise an error will be returned:
SELECT a, b, avg(temp)
FROM tab
GROUP BY a;
SELECT a, avg(temp)
FROM tab
GROUP BY a, b;
SELECT a, b, avg(temp)
FROM tab
GROUP BY a, b;