SLIDE 25 Hierarchies on Dimensions II
- Here is basic SQL code to accomplish this.
- For convenience, first create a view which provides the summarizaion
without the categorization:
CREATE VIEW By_Color_no_size AS SELECT DISTINCT item_name , SUM(dark) AS dark , SUM(pastel) AS pastel , SUM(white) AS white , SUM(dark )+ SUM(pastel )+ SUM(white) AS total FROM By_Color GROUP BY item_name UNION SELECT ’total ’, SUM(dark), SUM(pastel), SUM(white), SUM(dark )+ SUM(pastel )+ SUM(white) FROM By_Color;
- Now add the categorization:
SELECT category , item_name , dark , pastel , white , total FROM itemcategory NATURAL JOIN By_Color_no_size UNION SELECT category , ’subtotal ’, SUM(dark), SUM(pastel), SUM(white), SUM(total) FROM itemcategory NATURAL JOIN By_Color_no_size GROUP BY category UNION SELECT ’ztotal ’, ’’, SUM(dark), SUM(pastel), SUM(white), SUM(total) FROM itemcategory NATURAL JOIN By_Color_no_size ORDER BY 1,2;
Advanced SQL II — Advanced Aggregation and OLAP 20130417 Slide 25 of 35