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;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;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;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:
- Number of products of the same kind sold in a particular store
- Number of all the products sold within a store
- 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_ID, PRODUCT_ID , SUM(QUANTITY) AS TOTAL_QUANTITY FROM STORE_QTY GROUP BY ROLLUP (STORE_ID,PRODUCT_ID) ORDER BY 1 , 2;If you see the above output shows :
- 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.
- 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 ).
- 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 1 , 2;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
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.
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.
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!
Wow, this paragraph is pleasant, my sister is analyzing these kinds of things, therefore I am going to inform her.
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
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.
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!
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.
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…
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.
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
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!
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.
I like this post, enjoyed this one thanks for putting up.
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.
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.
https://shorturl.fm/G2uGS
https://shorturl.fm/8QP18
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!
https://shorturl.fm/lMjGp
I was checking out nilfortuneonline and I like what I’m seeing. I’ll probably be back on tomorrow. nilfortuneonline
https://shorturl.fm/zWVhf
https://shorturl.fm/OE4LH
https://shorturl.fm/HlsoO
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
https://shorturl.fm/YKiyH
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
https://shorturl.fm/mbpIr
https://shorturl.fm/vboK4
https://shorturl.fm/LQydL
https://shorturl.fm/rEPxy
https://shorturl.fm/g1heF
https://shorturl.fm/0zAoz
https://shorturl.fm/lQDHY
https://shorturl.fm/cx606
https://shorturl.fm/6rLQG
t50v9v
https://shorturl.fm/zPsLd
8s8lbi
Thanks Revanth.Yes this function is available in all the latest versions of Teradata
Nice Explanation. Is this function available in all versions of Teradata ?