Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

PostgreSQL: Group By ROLLUP

In PostgreSQL, the ROLLUP clause is a subclause of the GROUP BY clause that is shorthand for defining multiple grouping sets. Unlike the CUBE clause, the ROLLUP does not generate all the possible grouping sets based on a specified list of columns, but just makes a subset of those.

The ROLLUP defines a hierarchy between the list of columns defined and generates all grouping sets based on that hierarchy. This is commonly used for the analysis of hierarchical data; e.g., total salary by department, gender, and company-wide total.

Syntax:
SELECT
    <column1>,
    <column2>
FROM <table_name>
GROUP BY
    ROLLUP(<column1>,<column2>...)
[ORDER BY &lt;column_list<];

For example, the CUBE(c1,c2,c3) will generate eight possible combinations of grouping sets as shown below.

Example:
(c1,c2,c3)
(c1,c2)
(c1,c3)
(c2,c3)
(c1)
(c2)
(c3)
()

The ROLLUP(c1,c2,c3) in GROUP BY will consider column hierarchy as c1 -> c2 -> c3 and will generate four hierarchical grouping sets, as shown below.

Example: ROLLUP Grouping Sets
(c1,c2,c3)
(c1,c2)
(c1)
()

Let’s understand how ROLLUP works using the following employee table.

The following query uses the ROLLUP clause to find the total salary of employees, the total salary of employees by dept_id, and the total salary of employees (subtotal) by dept_id and gender.

Example: GROUP BY ROLLUP
SELECT dept_id, gender, SUM(salary) 
FROM employee 
GROUP BY
	ROLLUP(dept_id,gender)
ORDER BY dept_id, gender;

As you can see above, the ROLLUP clause takes hierarchy from dept_id -> gender and show grouping set for (dept_id, gender), (dept_id), and ( ).

If you change the order of columns gender and dept_id in the select statement and ROLLUP, the result will be different.

Example: ROLLUP
SELECT gender, dept_id, SUM(salary) 
FROM employee 
GROUP BY
	ROLLUP(gender,dept_id)
ORDER BY gender, dept_id;

As you can see above, now the driving column in the hierarchy is gender. Hence it shows the result of all grouping sets (gender, dept_id), (gender), and ( ).

It is also possible to do a partial rollup to reduce the number of subtotals generated.

Example: Partial ROLLUP
SELECT dept_id, gender, SUM(salary) 
FROM employee 
GROUP BY
	dept_id,
	ROLLUP(gender)
ORDER BY dept_id, gender;

The above query does partial rollup of grouping sets (dept_id, gender) and (dept_id).

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.