Comments on: Dynamic and Conditional grouping in SQL queries – for flexible results from single query (oh and a useful case for the CUBE operator) http://technology.amis.nl/2008/11/22/dynamic-and-conditional-grouping-in-sql-queries-for-flexible-results-from-single-query-oh-and-a-useful-case-for-the-cube-operator/ Friends of Oracle and Java Tue, 21 Oct 2014 18:48:09 +0000 hourly 1 http://wordpress.org/?v=4.0 By: Rob van Wijk http://technology.amis.nl/2008/11/22/dynamic-and-conditional-grouping-in-sql-queries-for-flexible-results-from-single-query-oh-and-a-useful-case-for-the-cube-operator/#comment-5583 Mon, 24 Nov 2008 20:04:52 +0000 http://technology.amis.nl/blog/?p=4162#comment-5583 Hi Lucas,

You can use CUBE as an inefficient row generator (“select rownum from (select 1 from dual group by cube(1,1,1,1)) where rownum <= 13″) or maybe for some obscure BI query or report. I’ll have to ask this to some BI colleagues. However, you said it perfectly: “CUBE will aggregate along all dimensions and at all levels – while we will always need just a subset”. I think CUBE has become almost obsolete for normal development, since the advent of grouping sets. With grouping sets you can specify exactly which grouping you want, without having to calculate everything first and then filter.

Regards,
Rob.

]]>
By: Lucas Jellema http://technology.amis.nl/2008/11/22/dynamic-and-conditional-grouping-in-sql-queries-for-flexible-results-from-single-query-oh-and-a-useful-case-for-the-cube-operator/#comment-5582 Mon, 24 Nov 2008 05:02:31 +0000 http://technology.amis.nl/blog/?p=4162#comment-5582 Hi Rob,
Thanks for your response. Yes, you are right on both accounts. I did not use the HAVING because of the way in which I expect the application to make use of this query. Manipulating the where-clause is much easier in the application than fiddling with the HAVING clause, that’s why I approached it like this. And of course the CUBE will aggregate along all dimensions and at all levels – while we will always need just a subset. How costly that proves to be? The percentage unnecessary aggregations can grow quite large, depending on the logic in the application – up to 95% or more. So perhaps this is not a very good example of CUBE usage after all. Do you know one?
regards, Lucas

]]>
By: Rob van Wijk http://technology.amis.nl/2008/11/22/dynamic-and-conditional-grouping-in-sql-queries-for-flexible-results-from-single-query-oh-and-a-useful-case-for-the-cube-operator/#comment-5581 Sat, 22 Nov 2008 13:20:40 +0000 http://technology.amis.nl/blog/?p=4162#comment-5581 Lucas,

Nice post.
Two remarks though:
1) the CUBE query will look much easier when using the HAVING clause, since there won’t be a need for the inline view in that case.
2) the CUBE query will be less performant than your first query as it has to calculate all grouping sets first before being able to filter on them. To verify: in the explain plan you’ll see a “GENERATE CUBE” step with a much larger number in the A-rows column than the number of rows in your table.

Regards,
Rob.

]]>