This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing a demonstration of PostgreSQL GROUP BY ROLLUP which we are using for data analytics purpose.
The data analytics is a huge area and there are hundreds of tools available in the market. OLTP System like PostgreSQL is not made for Data Analytics but supports medium level of data analytics on real-time data.
ROLLUP is generally used for analysis over hierarchical data like generating sub-total and grand-total.
Check the below example:
Create a table with sample data:
1 2 3 4 5 6 |
CREATE TABLE tbl_ProductSales (ID INT, Product_Category CHARACTER VARYING, Product_Name CHARACTER VARYING, TotalSales INT); INSERT INTO tbl_ProductSales VALUES (1,'Game','Mobo Game',200),(2,'Game','PKO Game',400) ,(3,'Fashion','Shirt',500),(4,'Fashion','Shorts',100); |
Check the ROLLUP result:
1 2 3 4 5 6 7 |
SELECT Product_Category ,Product_Name ,SUM(TotalSales) AS TotalSales FROM tbl_ProductSales GROUP BY ROLLUP (Product_Category, Product_Name) ORDER BY Product_Category, Product_Name |
PostgreSQL: Multiple GROUP BY using GROUPING SETS in Single SQL Query
Leave a Reply