Bird
0
0

How can you combine GROUPING SETS with ROLLUP to get totals by department, and by department-month with subtotals for department and grand total?

hard📝 Application Q9 of 15
PostgreSQL - Aggregate Functions and GROUP BY
How can you combine GROUPING SETS with ROLLUP to get totals by department, and by department-month with subtotals for department and grand total?
AUse GROUP BY CUBE(department, month)
BUse GROUP BY ROLLUP(department, month) only
CUse GROUPING SETS ((department), (month)) only
DUse GROUPING SETS ((department, month), (department), ()), combining ROLLUP and GROUPING SETS
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want totals by department-month, by department, and grand total (empty grouping).
  2. Step 2: Use GROUPING SETS with empty set for grand total

    GROUPING SETS ((department, month), (department), ()) includes all needed groupings and grand total.
  3. Final Answer:

    Use GROUPING SETS ((department, month), (department), ()), combining ROLLUP and GROUPING SETS -> Option D
  4. Quick Check:

    Empty parentheses in GROUPING SETS give grand total [OK]
Quick Trick: Empty set () in GROUPING SETS gives grand total [OK]
Common Mistakes:
  • Using ROLLUP alone misses explicit grouping sets
  • Forgetting empty set for grand total
  • Confusing CUBE with ROLLUP and GROUPING SETS

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes