November 29, 2017 at 10:49 pm
Comments posted to this topic are about the item The Difference Between Rollup and Cube
November 30, 2017 at 12:21 am
very interesting. The point is the order of fields in the GROUP BY ROLLUP, that establishes the hierarchy.
In order to get the same result you get with CUBE using ROLLUP, you need to merge two select in a UNION:
SELECT
coalesce (department, 'All Departments') AS Department,
coalesce (gender,'All Genders') AS Gender,
sum(salary) as Salary_Sum
FROM employee
GROUP BY ROLLUP (gender,department)
UNION
SELECT
coalesce (department, 'All Departments') AS Department,
coalesce (gender,'All Genders') AS Gender,
sum(salary) as Salary_Sum
FROM employee
GROUP BY ROLLUP (department,gender)
The two SELECTs are in all equal to each other, except for the order of fields in GROUP BY ROLLUP.
You can say that conceptually CUBE is more similar to GROUP BY, in the sense that the order of fields
is unrelevant. Using ROLLUP you must put some care in the effects of different permutations.
November 30, 2017 at 10:36 am
I have never used either of these functions but in my current position I can see lots of places where I can utilize them.
Thanks, great stuff.
November 30, 2017 at 12:38 pm
Nice right up. I've used these two functions to optimize a few procedures we have floating around. It resulted in a lesson to the rest of the staff on newer sql functions available and prompted us to update.
November 30, 2017 at 1:28 pm
bpwilso - Thursday, November 30, 2017 12:38 PMNice right up. I've used these two functions to optimize a few procedures we have floating around. It resulted in a lesson to the rest of the staff on newer sql functions available and prompted us to update.
NEW?!? Some version of CUBE/ROLLUP has been around since I started using SQL (6.5 or 7) back in 1999. The syntax changed when they introduced GROUPING SETS in SQL 2008(?), but the functionality hasn't really changed.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
November 30, 2017 at 5:41 pm
gfabbri - Thursday, November 30, 2017 12:21 AMvery interesting. The point is the order of fields in the GROUP BY ROLLUP, that establishes the hierarchy.
In order to get the same result you get with CUBE using ROLLUP, you need to merge two select in a UNION:SELECT
coalesce (department, 'All Departments') AS Department,
coalesce (gender,'All Genders') AS Gender,
sum(salary) as Salary_Sum
FROM employee
GROUP BY ROLLUP (gender,department)
UNION
SELECT
coalesce (department, 'All Departments') AS Department,
coalesce (gender,'All Genders') AS Gender,
sum(salary) as Salary_Sum
FROM employee
GROUP BY ROLLUP (department,gender)The two SELECTs are in all equal to each other, except for the order of fields in GROUP BY ROLLUP.
You can say that conceptually CUBE is more similar to GROUP BY, in the sense that the order of fields
is unrelevant. Using ROLLUP you must put some care in the effects of different permutations.
If you depend on the implicit return order of either, you could end up in deep Kimchi. Take a look at the GROUPING() function to see what can be done there.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 30, 2017 at 7:20 pm
This was removed by the editor as SPAM
December 2, 2017 at 12:09 pm
I think it is very interesting that you can group multiple hierarchies and can see where this can be very useful! Thank you so much for sharing, your awesome! Moreover, I love that you explain the differences!
December 4, 2017 at 6:13 am
Nice article.
The only thing I don't like is your use of COALESCE. If there happens to be a NULL department (as the result of a left join), then it will show up as "All Departments." There is a function named GROUPING that will let you test the group level. So instead of this:
coalesce (department, 'All Departments') AS Department,
You could use this:
CASE GROUPING(department) WHEN 1 THEN 'All Departments' ELSE department END AS Department
May 3, 2019 at 3:08 pm
So I have tried this query and I repeatedly get the error "Invalid column name". Everything I have read indicates that you can't use alias in the group by since the select is processed later in the order of the query. So what am I missing here that will make this work "as is"?
SELECT
coalesce (department, 'All Departments') AS Department,
coalesce (gender,'All Genders') AS Gender,
sum(salary) as Salary_Sum
FROM employee
GROUP BY ROLLUP (department, gender)
For help on getting help, follow the link below:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
May 3, 2019 at 5:16 pm
How the grouping and aliases works is correct but in this case you are grouping by columns that do exist. The example is the same as doing it without the aliases:
SELECT
coalesce (department, 'All Departments') ,
coalesce (gender,'All Genders'),
sum(salary) as Salary_Sum
FROM employee
GROUP BY ROLLUP (department, gender)
Are you executing against an employee table that has a department, salary and gender column? Same spelling? Or same case if needed depending on your collation?
Sue
May 3, 2019 at 5:26 pm
Removing the alias makes perfect sense, oh and it works perfectly! This will make my reporting a lot easier!
Thank you Sue for the insight and Ben for the article!
For help on getting help, follow the link below:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
May 4, 2019 at 9:20 am
Very useful post, I have never really fully understood cube but this post has helped me understand,, as it is explained clearly with good examples. I definately have some testing to do! Thanks for sharing
May 21, 2021 at 7:25 am
Really helpful in understanding the topic.The content is good and understandable.
May 21, 2021 at 2:02 pm
Really helpful in understanding the topic.The content is good and understandable.
Awesome!. What did you actually like about "the content" and how was it "really helpful"?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply