Átomo
51Gs
Agregação · SQL

GROUPING SETS

Abrir na tabela

Definição

Define exatamente quais agrupamentos calcular em uma única passada pela tabela.

Exemplos

1. Dois agrupamentos independentes
SELECT region, channel, SUM(amount) AS total
FROM sales
GROUP BY GROUPING SETS ((region), (channel));
2. Detalhe e total geral
SELECT region, SUM(amount) AS total
FROM sales
GROUP BY GROUPING SETS ((region), ());
3. Substituir vários UNION ALL
SELECT year, month, store_id, SUM(amount) AS total
FROM sales
GROUP BY GROUPING SETS ((year, month), (year, store_id), (year));

No mesmo grupo