DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Migrating From ClickHouse to Apache Doris: What Happened?
  • A New Era Has Come, and So Must Your Database Observability
  • Conversational Applications With Large Language Models Understanding the Sequence of User Inputs, Prompts, and Responses
  • Automating Database Operations With Ansible and DbVisualizer

Trending

  • Extracting Maximum Value From Logs
  • Creating a Custom Starter With Spring Boot 3
  • Information Security: AI Security Within the IoT Industry
  • How To Verify Database Connection From a Spring Boot Application
  1. DZone
  2. Data Engineering
  3. Databases
  4. T-SQL ROLLUP and CUBE

T-SQL ROLLUP and CUBE

CUBE generates a result set that shows aggregates for all combinations of values and ROLLUP generates a result set.

Joydeep Das user avatar by
Joydeep Das
·
Apr. 24, 15 · Interview
Like (0)
Save
Tweet
Share
21.10K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

First of all I salute all the new and rewarded MVPs in the month of April 2015. I congratulate them for their achievement. Hope the community can learn a lot of new things from them.

After my blog post on “Group By Grouping Set,” requests came from my friends circle to complete the article by providing something related to CUBE, ROLLUP and COMPUTE. So in this article we are trying to learn something related to it. Hope it will be informative.

CUBE and ROLLUP

Generally CUBE and ROLLUP are used for reporting purposes and they do the Subtotal and Grand total. CUBE generates a result set that shows aggregates for all combinations of values in the selected columns and ROLLUP generates a result set that shows aggregates for a hierarchy of values in the selected columns.

Let’s take an Example to understand it

We have a table like this.

ClassSection RollMarks
1 A 1 40
1 A 2 30
1 A 3 20
1 B 1 40
1 B 2 30
1 B 3 30
2 A 1 20
2 A 2 60
2 A 3 40
2 B 1 20
2 B 2 30
2 B 3 20

If we make the WITH ROLLUP we can get the Output like this

ClassSection Marks
1 A 90
1 B 100
1NULL19090 + 100
2 A 120
2 B 70
2NULL190120 + 70
NULLNULL380190 + 190

If we make the WITH CUBE we can get the Output like this

ClassSection Marks
1 A 90
1 B 100
1NULL19090 + 100
2 A 120
2 B 70
2NULL190120 + 70
NULLNULL380190 + 190
NULLA21090  +  120
NULLB170100 +  70

Let’s take a practical example

Step- 1 [ Create Base Table ]

 IF OBJECT_ID(N'[dbo].[tbl_EXAMPLETABLE]', N'U')IS NOT NULL
  BEGIN
  DROP TABLE [dbo].[tbl_EXAMPLETABLE];
  END
GO
CREATE TABLE [dbo].[tbl_EXAMPLETABLE]
  (
    CLASS  INT  NOT NULL,
    SECTION  CHAR(1)  NOT NULL,
    ROLL  INT  NOT NULL,
    MARKS  INT  NOT NULL
  );   

Step-2 [ Insert Records in Base Table ]

 INSERT INTO [dbo].[tbl_EXAMPLETABLE]
  (CLASS, SECTION, ROLL, MARKS)
VALUES(1,  'A', 1, 40),
  (1,  'A',  2, 30),
  (1,  'A',  3, 20),
  (1,  'B',  1, 40),
  (1,  'B',  2, 30),
  (1,  'B',  3, 30),
  (2,  'A',  1, 20),
  (2,  'A',  2, 60),
  (2,  'A',  3, 40),
  (2,  'B',  1, 20),
  (2,  'B',  2, 30),
  (2,  'B',  3, 20);

Step – 4 [ Make the WITH ROLLUP ]

 SELECT  CLASS, SECTION, SUM(MARKS) AS MARKS
FROM  [dbo].[tbl_EXAMPLETABLE]
GROUP BY CLASS, SECTION WITH ROLLUP;

Output

Step-5 [ Make the WITH CUBE  ]

 SELECT  CLASS, SECTION, SUM(MARKS) AS MARKS
FROM  [dbo].[tbl_EXAMPLETABLE]
GROUP BY CLASS, SECTION WITH CUBE;

Output

Hope you like it.

Database

Published at DZone with permission of Joydeep Das, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Migrating From ClickHouse to Apache Doris: What Happened?
  • A New Era Has Come, and So Must Your Database Observability
  • Conversational Applications With Large Language Models Understanding the Sequence of User Inputs, Prompts, and Responses
  • Automating Database Operations With Ansible and DbVisualizer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: