CUBE+GROUP BY for Subtotals
We all have used GROUP BY, but do you know CUBE?
For example, if I have a table of user spending, I can get the subtotal of each user AND the total spend of all users combined, all within one table.
π Syntax is here.
ππ΄π»π΄π²π
ππππ_ππ, πππΌ(πππππ) πππ
π΅ππΎπΌ πππππ
πΆππΎππΏ π±π π²ππ±π΄(ππππ_ππ)
This saves you the need to create separate queries for generating the total.
As usual, full example below!
Another similar function is ROLLUP. Can anyone explain the difference between ROLLUP and πΎππ½π?