| Having is less understood? After 20+ years of SQL usage (as an ordinary dev, not business/reporting/heavy SQL dev), I learned about `group by cube` from this article... "group by cube/coalesce" is much more complicated thing in this article than "having" (that could be explained as where but for group by) |
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...