ROLLUP Function with Example

Guest Post 

  • Author – Saurabh Gupta

 

In this post we will go through ROLLUP function which is one of the Extended grouping function.

ROLLUP is used to aggregate data along all the levels of hierarchy within a single dimension.

For example – 

In time dimension data can be aggregated along different levels like day, week, month and year. So if you need to display sale of a store for a particular day, month and year ( with grand totals ) , then you can first aggregate data on day and then roll up that data to month and then finally to year. In this case year is the highest level of roll up.

Let’s consider an example of one level of hierarchy:

Below is the STORE_QTY table which stores quantity of products sold on a particular day, where products are identified by PRODUCT_ID and Stores are identified by STORE_ID.

CREATE VOLATILE TABLE STORE_QTY

(
STORE_ID INTEGER,
SOLD_DATE DATE,
PRODUCT_ID INTEGER,
QUANTITY INTEGER
)
ON COMMIT PRESERVE ROWS; 

roll1

If you want to calculate the number of products of same kind sold from the table, along with the total number of products sold, then you can use a ROLLUP function as :

SELECT PRODUCT_ID
,SUM(QUANTITY)  AS TOTAL_QUANTITY
FROM STORE_QTY
GROUP BY ROLLUP  (PRODUCT_ID)
ORDER BY 1; 

 roll2

Here along with all Product_Id you get a row which has ? in PRODUCT_ID column.

This ? Isn’t null but instead indicates the grand total of all the product_id.

 

To separate actual null data and grand totals you can use a GROUPING function:

SELECT CASE GROUPING (PRODUCT_ID)  WHEN 1
THEN ‘TOTAL’ ELSE PRODUCT_ID END
PRODUCT_ID
SUM(QUANTITY)  AS TOTAL_QUANTITY
FROM STORE_QTY
GROUP BY ROLLUP  (PRODUCT_ID)
ORDER BY 1;

 roll3

Now if you see by using GROUPING function we have indicated grand total with Total

in above output instead of ?

 

Let’s consider another example where you have to ROLLUP data on two levels:


You have a store which sells different products and you want data to be displayed as:

  1. Number of products of the same kind sold in a particular store
  2. Number of all the products sold within a store
  3. Finally Number of products sold by all the stores

So basically here you need a ROLLUP first on PRODUCT_ID and then on STORE_ID.

SELECT STORE_IDPRODUCT_ID
SUM(QUANTITY)  AS  TOTAL_QUANTITY
FROM STORE_QTY
GROUP BY ROLLUP  (STORE_ID,PRODUCT_ID)
ORDER BY   2;

roll4

If you see the above output shows :

  1. Number of individual products sold in a particular store from rows 3 to 5 for store 100 and rows 7 to 8 for store 101.
  2. Number of all the products sold (indicated by ? In PRODUCT_ID column, you can use GROUPING to indicate Total Products ) in store 100 is shown in row 2 ( sum of row 3 to 5 ) whereas for store 101 it is shown in 6 ( sum of rows 7 to 8 ).
  3. Grand total of all the products sold across all the stores is indicated in row 1.

If you reverse the order of STORE_ID and PRODUCT_ID in your query then it will give you data firstly rolled up on STORE_ID and then on PRODUCT_ID

SELECT PRODUCT_ID , STORE_ID
,SUM (QUANTITY)  AS TOTAL_QUANTITY
FROM STORE_QTY
GROUP BY ROLLUP  (PRODUCT_ID STORE_ID)
ORDER BY 2;

roll5

To summarize you can use ROLLUP function where you need data to be aggregated along all the levels of hierarchy.

Feel free to write your thoughts/queries/doubts about the post in the comment section.

If you like the post do share it on your social networking sites with the below share button 🙂

41 comments

Skip to comment form

  1. I love what you guys are up too. This type of clever work and reporting! Keep up the awesome works guys I’ve incorporated you guys to my blogroll.

  2. I’m not sure why but this weblog is loading incredibly slow for me. Is anyone else having this problem or is it a issue on my end? I’ll check back later and see if the problem still exists.

  3. Hi! I’m at work browsing your blog from my new apple iphone! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the excellent work!

  4. Wow, this paragraph is pleasant, my sister is analyzing these kinds of things, therefore I am going to inform her.

  5. I just couldn’t go away your website prior to suggesting that I extremely enjoyed the usual information a person provide on your guests? Is going to be again frequently in order to investigate cross-check new posts

  6. Ahaa, its nice dialogue on the topic of this article here at this web site, I have read all that, so at this time me also commenting here.

  7. Pretty great post. I simply stumbled upon your blog and wanted to say that I’ve really loved surfing around your weblog posts. In any case I will be subscribing for your feed and I’m hoping you write once more soon!

  8. Hmm is anyone else experiencing problems with the images on this blog loading? I’m trying to determine if its a problem on my end or if it’s the blog. Any feed-back would be greatly appreciated.

  9. I needed to thank you for this fantastic read!! I certainly enjoyed every little bit of it. I have you saved as a favorite to check out new stuff you post…

  10. I will right away take hold of your rss as I can’t to find your e-mail subscription link or newsletter service. Do you have any? Kindly permit me know in order that I could subscribe. Thanks.

  11. I need to to thank you for this wonderful read!! I certainly loved every bit of it. I have you saved as a favorite to check out new things you

  12. That is a great tip particularly to those fresh to the blogosphere. Short but very accurate info… Thanks for sharing this one. A must read article!

  13. Thank you for your entire hard work on this site. Gloria really likes doing investigations and it’s really simple to grasp why. Almost all learn all about the powerful mode you make great guides on this website and in addition encourage participation from some others on that subject matter plus our favorite girl has been learning a whole lot. Take pleasure in the remaining portion of the year. You are conducting a glorious job.

  14. I like this post, enjoyed this one thanks for putting up.

  15. Ahaa, its fastidious dialogue concerning this piece of writing here at this web site, I have read all that, so at this time me also commenting here.

  16. Basic strategy’s all about minimizing losses, right? Seeing platforms like BetPK embrace responsible gaming with features like 2FA is smart. Plus, easy GCash deposits via the betpk app download apk make things convenient! Good to see localized options too.

    • Shirley1437 on January 9, 2026 at 2:37 pm
    • Reply

    https://shorturl.fm/G2uGS

    • Theresa3541 on January 9, 2026 at 11:44 am
    • Reply

    https://shorturl.fm/8QP18

  17. Excellent breakdown of ROLLUP! This hierarchical aggregation is exactly what gaming platforms need for multi-level analytics – daily player metrics rolling up to monthly trends, then yearly insights. I’ve implemented similar grouping functions for wk777‘s VIP tier reporting, and the CASE/GROUPING pattern you mention is crucial for distinguishing actual nulls from super-aggregate rows. Great practical example!

    • Devin4763 on December 29, 2025 at 10:09 am
    • Reply

    https://shorturl.fm/lMjGp

  18. I was checking out nilfortuneonline and I like what I’m seeing. I’ll probably be back on tomorrow. nilfortuneonline

    • Jermaine3131 on December 28, 2025 at 6:31 am
    • Reply

    https://shorturl.fm/zWVhf

    • Quentin910 on December 22, 2025 at 12:25 pm
    • Reply

    https://shorturl.fm/OE4LH

    • Jorge4742 on December 18, 2025 at 5:49 am
    • Reply

    https://shorturl.fm/HlsoO

  19. KYC verification on Fun88 through fun88fun88kyc.com was pretty straightforward. Good to know they’re taking security seriously. Now, time to play! Verify your account here: fun88fun88kyc

    • Lauren1271 on December 5, 2025 at 6:00 pm
    • Reply

    https://shorturl.fm/YKiyH

  20. Anybody win anything big on kkwim lately? Just curious because I keep seeing people advertise it. Looking for a place that I can potentially win with little effort. kkwim

    • Alma4313 on December 2, 2025 at 2:46 am
    • Reply

    https://shorturl.fm/mbpIr

    • Elise4216 on November 30, 2025 at 11:07 am
    • Reply

    https://shorturl.fm/vboK4

    • Wyatt2257 on November 27, 2025 at 5:23 pm
    • Reply

    https://shorturl.fm/LQydL

    • Bill4599 on November 24, 2025 at 6:22 pm
    • Reply

    https://shorturl.fm/rEPxy

    • Luca1822 on November 24, 2025 at 3:17 pm
    • Reply

    https://shorturl.fm/g1heF

    • Joy1971 on November 24, 2025 at 5:10 am
    • Reply

    https://shorturl.fm/0zAoz

    • Clementine1532 on November 21, 2025 at 6:27 am
    • Reply

    https://shorturl.fm/lQDHY

    • Hermione1442 on November 14, 2025 at 4:20 pm
    • Reply

    https://shorturl.fm/cx606

    • Mark3345 on November 12, 2025 at 6:14 pm
    • Reply

    https://shorturl.fm/6rLQG

    • Marc4505 on November 1, 2025 at 5:03 am
    • Reply

    https://shorturl.fm/zPsLd

    • Admin on August 12, 2015 at 5:36 pm
    • Reply

    Thanks Revanth.Yes this function is available in all the latest versions of Teradata

  21. Nice Explanation. Is this function available in all versions of Teradata ?

Leave a Reply to Shirley1437 Cancel reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.