Skip to content
OCTAVIIUS DOCSCore & Studio 0.2.83
SQL reference / 0.2.83

Aggregation, distinct cardinality and SQL windows

GROUP BY, HAVING, exact aggregates and window calculations with partitions and deterministic ordering.

Grain and aggregation state

GROUP BY defines result grain. WHERE reduces rows before aggregation; HAVING filters the resulting groups. COUNT(*) counts rows, COUNT(expression) counts non-NULL values, and COUNT(DISTINCT expression) counts distinct non-NULL values. Working memory depends on group count and retained state. For DISTINCT, distinct values per group add another cost dimension.

Group aggregates and NULL

COUNT(*) counts rows; COUNT(expression) ignores NULL; COUNT(DISTINCT expression) counts distinct non-NULL values. COUNT_BIG is accepted. SUM/AVG/MIN/MAX ignore NULL. Without GROUP BY, an empty input yields one row: COUNT is 0 and ordinary value aggregates are NULL. With GROUP BY, an empty input has no groups.

AVG over an integer expression keeps integer behavior here (truncated division); CAST to DECIMAL or FLOAT for a fractional average. STDEV/VAR are sample variants, STDEVP/VARP population variants. STRING_AGG and CHECKSUM_AGG have their own bounds and do not imply bit-identical behavior to another DBMS.

SQL
SELECT Pays, COUNT(*) AS lignes, COUNT(Montant) AS valeurs,
       SUM(Montant) AS total, AVG(Montant) AS moyenne,
       MIN(Montant) AS minimum, MAX(Montant) AS maximum,
       COUNT(DISTINCT ClientId) AS clients
FROM dbo.Ventes GROUP BY Pays HAVING COUNT(*) >= 1 ORDER BY Pays;

ROLLUP, CUBE and GROUPING SETS

GROUP BY ROLLUP, CUBE and GROUPING SETS, and WITH ROLLUP/WITH CUBE forms, produce subtotal levels. GROUPING and GROUPING_ID distinguish subtotal NULL from data NULL. Statistical/string reducers and some subtotal paths retain memory guards; spill support is not universal.

SQL
SELECT Pays, SUM(Montant) AS total, GROUPING(Pays) AS sous_total
FROM dbo.Ventes GROUP BY ROLLUP(Pays);

Ranking, offsets and running calculations

Known 0.2.83 defect: an ordinary window over a table can fail with NameError: table is not defined. This was reproduced on the fixture. The example below documents accepted syntax, not a verified operational recipe. Read known issues before relying on windows.

Window expressions include ROW_NUMBER, RANK, DENSE_RANK, NTILE, LAG, LEAD and SUM/COUNT/AVG/MIN/MAX with OVER. PARTITION BY defines partitions; ORDER BY defines calculation order. Add tie-breakers for deterministic ROW_NUMBER/LAG/LEAD. A window does not imply final SELECT ordering.

Supported frames run from UNBOUNDED PRECEDING to CURRENT ROW or UNBOUNDED FOLLOWING, with ROWS/RANGE. Bounded n PRECEDING/FOLLOWING and DISTINCT window aggregates are rejected. Use a derived table to combine grouped aggregation and windows.

SQL
SELECT Id, Pays,
 ROW_NUMBER() OVER (PARTITION BY Pays ORDER BY Id) AS rang,
 SUM(Montant) OVER (PARTITION BY Pays ORDER BY Id
   ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumul
FROM dbo.Ventes ORDER BY Id;

Exactness and performance

DISTINCT results are exact; bitmap/chunk unions are not probabilistic estimates. The engine may reuse a compatible preaggregate or snapshot-bound result. Cached result timings are not comparable to computations after restart. Process count depends on available resources and the plan, not a column name or SQL-hardcoded core count.

Aggregate ratios and explicit precision

The ratio is calculated at group grain. NULLIF handles a zero denominator and CAST fixes fractional division independently of the integer count type. DISTINCT remains exact. In the reference dataset, France has two non-NULL measures totaling 20.0000 and Portugal one measure totaling 30.0000.

SQL
SELECT Pays, COUNT(*) AS NbLignes,
       COUNT(Montant) AS NbValeurs,
       COUNT(DISTINCT ClientId) AS NbClients,
       SUM(Montant) AS Total,
       CAST(SUM(Montant) AS DECIMAL(19,4))
         / NULLIF(CAST(COUNT(Montant) AS DECIMAL(19,4)), 0)
         AS MoyenneValeurs
FROM dbo.Ventes
GROUP BY Pays
ORDER BY Pays;