|
I had never heard of GROUP BY CUBE either! It looks like it's part of a family of special GROUP BY operators—GROUPING SETS, CUBE, and ROLLUP—that basically issue the same query multiple times with different GROUP BY expressions and UNION the results together. Using GROUP BY CUBE(a, b, c, ...) creates GROUP BY expressions for every element in the power set of {a, b, c, ...}, so GROUP BY CUBE(a, b) does separate GROUP BYs for (a, b), (a), (b) and (). It's like SQL's version of a pivot table, returning aggregations of data filtered along multiple dimensions, and then also the aggregations of those aggregations. It seems like it's well supported by Postgres [1], SQL Server [2] and Oracle [3], but MySQL only has partial support for ROLLUP with a different syntax [4]. [1]: https://www.postgresql.org/docs/current/queries-table-expres... [2]: https://docs.microsoft.com/en-us/sql/t-sql/queries/select-gr... [3]: https://oracle-base.com/articles/misc/rollup-cube-grouping-f... [4]: https://dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.h... |
Is there such a book?