Get subtotals with ROLLUP+GROUP BY
ππππππ generates extra rows that represent the subtotals.
For exampleβ¦
To get the sum of spending of each user, you typically GROUP BY user_id and SUM spend.
Want to get the sum of ALL users in the same result? Try ROLLUP.
π The syntax is here.
ππ΄π»π΄π²π
ππππ_ππ, πππΌ(πππππ)
π΅ππΎπΌ
πππππ
πΆππΎππΏ π±π ππΎπ»π»ππΏ(ππππ_ππ)
The result will contain a NULL user_id which contains the sum of spend of all users.
π In the demo below, we do a roll-up of two aggregation columns, and show the use of a partial roll-up. Check it out.
Bonus point if you can spot the mistake in the demo below.