← Back to PostgreSQL Course | Chapter 7: Aggregations & Grouping | Lesson 6 of 7

GROUPING SETS का उपयोग

GROUPING SETS आपको एक query में कई अलग groupings compute करने देता है।

In this page:

  1. GROUPING SETS
Syntax
sql
SELECT column1, column2, aggregate_function(column)
FROM table_name
GROUP BY GROUPING SETS ((column1), (column2), ());

GROUPING SETS

GROUP BY GROUPING SETS ((a), (b), ()) एक single pass में a से, b से, और overall totals compute करता है।

CUBE (a, b) listed columns का हर combination generate करता है और ROLLUP एक hierarchy। एक grouping से belong न करने वाली rows में उस column में NULL है।

यह कई UNION ALL queries की जगह लेता है।

Note: GROUPING SETS UNION ALL से joined कई GROUP BY queries की जगह लेता है।

उदाहरण: GROUPING SETS

bash
shop=# SELECT region, item, SUM(qty) AS total
shop-# FROM sales GROUP BY GROUPING SETS ((region), (item), ())
shop-# ORDER BY region, item;
 region | item | total
--------+------+-------
 N      |      |     8
 S      |      |    13
        | book |     4
        | lamp |     2
        | pen  |    15
        |      |    21

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. हर grouping के लिए separate queries लिखना
  2. NULL placeholder rows label न करना
  3. कई columns पर CUBE इस्तेमाल करना
चैप्टर सारांश
  • एक query में कई groupings
  • CUBE सारे combinations देता है
  • ROLLUP एक hierarchy देता है
  • Missing groups NULL दिखाती हैं
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.