<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>OpenText Analytics Database 26.2.x – GROUP BY clause</title>
    <link>/en/sql-reference/statements/select/group-by-clause/</link>
    <description>Recent content in GROUP BY clause on OpenText Analytics Database 26.2.x</description>
    <generator>Hugo -- gohugo.io</generator>
    
	  <atom:link href="/en/sql-reference/statements/select/group-by-clause/index.xml" rel="self" type="application/rss+xml" />
    
    
      
        
      
    
    
    <item>
      <title>Sql-Reference: CUBE aggregate</title>
      <link>/en/sql-reference/statements/select/group-by-clause/cube-aggregate/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/statements/select/group-by-clause/cube-aggregate/</guid>
      <description>
        
        
        &lt;p&gt;Automatically performs all possible aggregations of the specified columns, as an extension to the &lt;a href=&#34;../../../../../en/sql-reference/statements/select/group-by-clause/&#34;&gt;GROUP BY&lt;/a&gt; clause.&lt;/p&gt;
&lt;p&gt;You can use the ROLLUP clause with three grouping functions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;

&lt;code&gt;&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/grouping/#&#34;&gt;GROUPING&lt;/a&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;

&lt;code&gt;&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/group-id/#&#34;&gt;GROUP_ID&lt;/a&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;

&lt;code&gt;&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/grouping-id/#&#34;&gt;GROUPING_ID&lt;/a&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;GROUP BY &lt;span class=&#34;code-variable&#34;&gt;group-expression&lt;/span&gt;[,...]
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;parameters&#34;&gt;Parameters&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;group-expression&lt;/code&gt;&lt;/em&gt;``&lt;/dt&gt;
&lt;dd&gt;One or both of the following:
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;An expression that is not an aggregate or a grouping function that includes constants and column references in &lt;code&gt;FROM&lt;/code&gt;-specified tables. For example:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;column1, (column2+1), column3+column4
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A multilevel expression, one of the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;ROLLUP&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;CUBE&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;GROUPING SETS&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;restrictions&#34;&gt;Restrictions&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;GROUP BY CUBE does not order data. If you want to sort data, use the &lt;a href=&#34;../../../../../en/sql-reference/statements/select/order-by-clause/#&#34;&gt;ORDER BY clause&lt;/a&gt;. The ORDER BY clause must come &lt;em&gt;after&lt;/em&gt; the GROUP BY clause.&lt;/li&gt;
&lt;li&gt;You can use CUBE inside a GROUPING SETS expression, but not inside a ROLLUP expression or another CUBE expression.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;levels-of-cube-aggregation&#34;&gt;Levels of CUBE aggregation&lt;/h2&gt;
&lt;p&gt;If &lt;em&gt;n&lt;/em&gt; is the number of grouping columns, CUBE creates &lt;em&gt;2&lt;/em&gt;&lt;sup&gt;n&lt;/sup&gt; levels of aggregations. For example:&lt;/p&gt;
&lt;p&gt;CUBE (A, B, C) creates all possible groupings, resulting in eight groups:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;(A, B, C)&lt;/li&gt;
&lt;li&gt;(A, B)&lt;/li&gt;
&lt;li&gt;(A, C)&lt;/li&gt;
&lt;li&gt;(B, C)&lt;/li&gt;
&lt;li&gt;(A)&lt;/li&gt;
&lt;li&gt;(B)&lt;/li&gt;
&lt;li&gt;(C)&lt;/li&gt;
&lt;li&gt;()&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you increase the number of CUBE columns, the number of CUBE groupings increases exponentially. The CUBE query may be resource intensive and produce combinations that are not of interest. In that case, consider using the&lt;a href=&#34;../../../../../en/sql-reference/statements/select/group-by-clause/grouping-sets-aggregate/#&#34;&gt;GROUPING SETS aggregate&lt;/a&gt;, which allows you to choose specific groupings.&lt;/p&gt;
&lt;h2 id=&#34;examples&#34;&gt;Examples&lt;/h2&gt;
&lt;h3 id=&#34;using-cube-to-return-all-groupings&#34;&gt;Using CUBE to return all groupings&lt;/h3&gt;
&lt;p&gt;Suppose you have a table that contains information about family expenses for books and electricity:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM expenses ORDER BY Category, Year;
 Year |  Category   | Amount
------+-------------+--------
2005  | Books       |  39.98
2007  | Books       |  29.99
2008  | Books       |  29.99
2005  | Electricity | 109.99
2006  | Electricity | 109.99
2007  | Electricity | 229.98
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To aggregate the data by both Category and Year using the CUBE aggregate:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT Category, Year, SUM(Amount) FROM expenses
   GROUP BY CUBE(Category, Year) ORDER BY 1, 2, GROUPING_ID();
 Category    | Year |  SUM
-------------+------+--------
 Books       | 2005 |  39.98
 Books       | 2007 |  29.99
 Books       | 2008 |  29.99
 Books       |      |  99.96
 Electricity | 2005 | 109.99
 Electricity | 2006 | 109.99
 Electricity | 2007 | 229.98
 Electricity |      | 449.96
             | 2005 | 149.97
             | 2006 | 109.99
             | 2007 | 259.97
             | 2008 |  29.99
             |      | 549.92
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The results include subtotals for each category and year, and a grand total ($549.92).&lt;/p&gt;
&lt;h3 id=&#34;using-cube-with-the-having-clause&#34;&gt;Using CUBE with the HAVING clause&lt;/h3&gt;
&lt;p&gt;This example shows how you can restrict the GROUP BY results, use the HAVING clause with the CUBE aggregate. This query returns only the category totals and the full total:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT Category, Year, SUM(Amount) FROM expenses
   GROUP BY CUBE(Category,Year) HAVING GROUPING(Year)=1;
 Category    | Year |  SUM
-------------+------+--------
 Books       |      |  99.96
 Electricity |      | 449.96
             |      | 549.92
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The next query returns only the aggregations for the two categories for each year. The GROUPING ID function specifies to omit the grand total ($549.92):&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT Category, Year, SUM (Amount) FROM expenses
   GROUP BY CUBE(Category,Year) HAVING GROUPING_ID(Category,Year)&amp;lt;2
   ORDER BY 1, 2, GROUPING_ID();
 Category   | Year |  SUM
------------+------+--------
 Books      | 2005 |  39.98
 Books      | 2007 |  29.99
 Books      | 2008 |  29.99
 Books      |      |  99.96
 Electrical | 2005 | 109.99
 Electrical | 2006 | 109.99
 Electrical | 2007 | 229.98
 Electrical |      | 449.96
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;see-also&#34;&gt;See also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/data-analysis/data-aggregation/#&#34;&gt;Data aggregation&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/statements/select/group-by-clause/#&#34;&gt;GROUP BY clause&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/group-id/#&#34;&gt;GROUP_ID&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/grouping/#&#34;&gt;GROUPING&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/grouping-id/#&#34;&gt;GROUPING_ID&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/statements/select/group-by-clause/grouping-sets-aggregate/#&#34;&gt;GROUPING SETS aggregate&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/statements/select/group-by-clause/rollup-aggregate/#&#34;&gt;ROLLUP aggregate&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Sql-Reference: GROUPING SETS aggregate</title>
      <link>/en/sql-reference/statements/select/group-by-clause/grouping-sets-aggregate/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/statements/select/group-by-clause/grouping-sets-aggregate/</guid>
      <description>
        
        
        &lt;p&gt;The &lt;code&gt;GROUPING SETS&lt;/code&gt; aggregate is an extension to the 
&lt;code&gt;&lt;a href=&#34;../../../../../en/sql-reference/statements/select/group-by-clause/#&#34;&gt;GROUP BY&lt;/a&gt;&lt;/code&gt; clause that automatically performs subtotal aggregations on groupings that you specify.&lt;/p&gt;
&lt;p&gt;You can use the &lt;code&gt;GROUPING SETS&lt;/code&gt; clause with three grouping functions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;

&lt;code&gt;&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/grouping/#&#34;&gt;GROUPING&lt;/a&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;

&lt;code&gt;&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/group-id/#&#34;&gt;GROUP_ID&lt;/a&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;

&lt;code&gt;&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/grouping-id/#&#34;&gt;GROUPING_ID&lt;/a&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To sort data, use the 
&lt;code&gt;&lt;a href=&#34;../../../../../en/sql-reference/statements/select/order-by-clause/#&#34;&gt;ORDER BY&lt;/a&gt;&lt;/code&gt; clause. The &lt;code&gt;ORDER BY&lt;/code&gt; clause must follow the &lt;code&gt;GROUP BY&lt;/code&gt; clause.&lt;/p&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;GROUP BY &lt;span class=&#34;code-variable&#34;&gt;group-expression&lt;/span&gt;[,...]
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;parameters&#34;&gt;Parameters&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;group-expression&lt;/code&gt;&lt;/em&gt;``&lt;/dt&gt;
&lt;dd&gt;One or both of the following:
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;An expression that is not an aggregate or a grouping function that includes constants and column references in &lt;code&gt;FROM&lt;/code&gt;-specified tables. For example:&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;column1, (column2+1), column3+column4&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A multilevel expression, one of the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;ROLLUP&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;CUBE&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;GROUPING SETS&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;defining-the-groupings&#34;&gt;Defining the groupings&lt;/h2&gt;
&lt;p&gt;GROUPING SETS allows you to specify exactly which groupings you want in the results. You can also concatenate the groupings as follows:&lt;/p&gt;
&lt;p&gt;The following example clauses result in the groupings shown.

&lt;table class=&#34;table table-bordered&#34; &gt;



&lt;tr&gt; 

&lt;th &gt;
This clause...&lt;/th&gt; 

&lt;th &gt;
Defines groupings...&lt;/th&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;


&lt;code&gt;...GROUP BY GROUPING SETS(A,B,C,D)...&lt;/code&gt;&lt;/td&gt; 

&lt;td &gt;
(A), (B), (C), (D)&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;


&lt;code&gt;...GROUP BY GROUPING SETS((A),(B),(C),(D))...&lt;/code&gt;&lt;/td&gt; 

&lt;td &gt;
(A), (B), (C), (D)&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;


&lt;code&gt;...GROUP BY GROUPING SETS((A,B,C,D))...&lt;/code&gt;&lt;/td&gt; 

&lt;td &gt;
(A, B, C, D)&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;


&lt;code&gt;...GROUP BY GROUPING SETS(A,B),GROUPING SETS(C,D)...&lt;/code&gt;&lt;/td&gt; 

&lt;td &gt;
(A, C), (B, C), (A, D), (B, C)&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;


&lt;code&gt;...GROUP BY GROUPING SETS((A,B)),GROUPING SETS(C,D)...&lt;/code&gt;&lt;/td&gt; 

&lt;td &gt;
(A, B, C), (A, B, D)&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;


&lt;code&gt;...GROUP BY GROUPING SETS(A,B),GROUPING SETS(ROLLUP(C,D))...&lt;/code&gt;&lt;/td&gt; 

&lt;td &gt;
(A,B), (A,B,C), (A,B,C,D)&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;


&lt;code&gt;...GROUP BY A,B,C,GROUPING SETS(ROLLUP(C, D))...&lt;/code&gt;&lt;/td&gt; 

&lt;td &gt;




&lt;p&gt;(A, B, C, D), (A, B, C), (A, B, C)&lt;/p&gt;
&lt;p&gt;The clause contains two groups (A, B, C). In the HAVING clause, use the GROUP_ID function as a predicate, to eliminate the second grouping.&lt;/p&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/p&gt;
&lt;h2 id=&#34;example-selecting-groupings&#34;&gt;Example: selecting groupings&lt;/h2&gt;
&lt;p&gt;This example shows how to select only those groupings you want. Suppose you want to aggregate on columns only, and you do not need the grand total. The first query omits the total. In the second query, you add () to the GROUPING SETS list to get the total. Use the ORDER BY clause to sort the results by grouping:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT Category, Year, SUM(Amount) FROM expenses
   GROUP BY GROUPING SETS((Category, Year), (Year))
   ORDER BY 1, 2, GROUPING_ID();
  Category  | Year |  SUM
------------+------+--------
 Books      | 2005 |  39.98
 Books      | 2007 |  29.99
 Books      | 2008 |  29.99
 Electrical | 2005 | 109.99
 Electrical | 2006 | 109.99
 Electrical | 2007 | 229.98
            | 2005 | 149.97
            | 2006 | 109.99
            | 2007 | 259.97
            | 2008 |  29.99
=&amp;gt; SELECT Category, Year, SUM(Amount) FROM expenses
   GROUP BY GROUPING SETS((Category, Year), (Year), ())
   ORDER BY 1, 2, GROUPING_ID();
  Category  | Year |  SUM
------------+------+--------
 Books      | 2005 |  39.98
 Books      | 2007 |  29.99
 Books      | 2008 |  29.99
 Electrical | 2005 | 109.99
 Electrical | 2006 | 109.99
 Electrical | 2007 | 229.98
            | 2005 | 149.97
            | 2006 | 109.99
            | 2007 | 259.97
            | 2008 |  29.99
            |      | 549.92
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;see-also&#34;&gt;See also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/data-analysis/data-aggregation/#&#34;&gt;Data aggregation&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/statements/select/group-by-clause/cube-aggregate/#&#34;&gt;CUBE aggregate&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/grouping/#&#34;&gt;GROUPING&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/group-id/#&#34;&gt;GROUP_ID&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/grouping-id/#&#34;&gt;GROUPING_ID&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/statements/select/group-by-clause/#&#34;&gt;GROUP BY clause&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/statements/select/group-by-clause/rollup-aggregate/#&#34;&gt;ROLLUP aggregate&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Sql-Reference: ROLLUP aggregate</title>
      <link>/en/sql-reference/statements/select/group-by-clause/rollup-aggregate/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/sql-reference/statements/select/group-by-clause/rollup-aggregate/</guid>
      <description>
        
        
        &lt;p&gt;Automatically performs subtotal aggregations as an extension to the &lt;a href=&#34;../../../../../en/sql-reference/statements/select/group-by-clause/&#34;&gt;GROUP BY&lt;/a&gt; clause. &lt;code&gt;ROLLUP&lt;/code&gt; performs these aggregations across multiple dimensions, at different levels, within a single SQL query.&lt;/p&gt;
&lt;p&gt;You can use the &lt;code&gt;ROLLUP&lt;/code&gt; clause with three grouping functions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;

&lt;code&gt;&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/grouping/#&#34;&gt;GROUPING&lt;/a&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;

&lt;code&gt;&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/group-id/#&#34;&gt;GROUP_ID&lt;/a&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;

&lt;code&gt;&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/grouping-id/#&#34;&gt;GROUPING_ID&lt;/a&gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;syntax&#34;&gt;Syntax&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;ROLLUP &lt;/code&gt;&lt;em&gt;&lt;code&gt;grouping-expression&lt;/code&gt;&lt;/em&gt;&lt;code&gt;[,...]&lt;/code&gt;&lt;/p&gt;
&lt;h2 id=&#34;parameters&#34;&gt;Parameters&lt;/h2&gt;
&lt;dl&gt;
&lt;dt&gt;&lt;em&gt;&lt;code&gt;group-expression&lt;/code&gt;&lt;/em&gt;&lt;/dt&gt;
&lt;dd&gt;One or both of the following:
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;An expression that is not an aggregate or a grouping function that includes constants and column references in &lt;code&gt;FROM&lt;/code&gt;-specified tables. For example:&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;column1, (column2+1), column3+column4&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;A multilevel expression, one of the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;ROLLUP&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;CUBE&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;GROUPING SETS&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;h2 id=&#34;restrictions&#34;&gt;Restrictions&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;GROUP BY ROLLUP&lt;/code&gt; does not sort results. To sort data, an &lt;a href=&#34;../../../../../en/sql-reference/statements/select/order-by-clause/&#34;&gt;&lt;code&gt;ORDER BY&lt;/code&gt; clause&lt;/a&gt; must follow the &lt;code&gt;GROUP BY&lt;/code&gt; clause.&lt;/p&gt;
&lt;h2 id=&#34;levels-of-aggregation&#34;&gt;Levels of aggregation&lt;/h2&gt;
&lt;p&gt;If &lt;em&gt;n&lt;/em&gt; is the number of grouping columns, &lt;code&gt;ROLLUP&lt;/code&gt; creates &lt;em&gt;n&lt;/em&gt;+1 levels of subtotals and grand total. Because &lt;code&gt;ROLLUP&lt;/code&gt; removes the right-most column at each step, specify column order carefully.&lt;/p&gt;
&lt;p&gt;Suppose that &lt;code&gt;ROLLUP(A, B, C)&lt;/code&gt; creates four groups:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;(A, B, C)&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;(A, B)&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;(A)&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;()&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Because &lt;code&gt;ROLLUP&lt;/code&gt; removes the right-most column at each step, there are no groups for &lt;code&gt;(A, C)&lt;/code&gt; and &lt;code&gt;(B, C)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If you enclose two or more columns in parentheses, &lt;code&gt;GROUP BY&lt;/code&gt; treats them as a single entity. For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;ROLLUP(A, B, C)&lt;/code&gt; creates four groups:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
(A, B, C)
(A, B)
(A)
()
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;ROLLUP((A, B), C)&lt;/code&gt; treats &lt;code&gt;(A, B)&lt;/code&gt; as a single entity and creates three groups:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;(A, B, C)
(A, B)
()
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;example-aggregating-the-full-data-set&#34;&gt;Example: aggregating the full data set&lt;/h2&gt;
&lt;p&gt;The following example shows how to use the &lt;code&gt;GROUP BY&lt;/code&gt; clause to determine family expenses for electricity and books over several years. The &lt;code&gt;SUM&lt;/code&gt; aggregate function computes the total amount of money spent in each category per year.&lt;/p&gt;
&lt;p&gt;Suppose you have a table that contains information about family expenses for books and electricity:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT * FROM expenses ORDER BY Category, Year;
 Year |  Category   | Amount
------+-------------+--------
2005  | Books       |  39.98
2007  | Books       |  29.99
2008  | Books       |  29.99
2005  | Electricity | 109.99
2006  | Electricity | 109.99
2007  | Electricity | 229.98
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For the &lt;code&gt;expenses&lt;/code&gt; table, &lt;code&gt;ROLLUP&lt;/code&gt; computes the subtotals in each category between 2005–2007:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Books: $99.96&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Electricity: $449.96&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Grand total: $549.92.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Use the &lt;code&gt;ORDER BY&lt;/code&gt; clause to sort the results:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT Category, Year, SUM(Amount) FROM expenses
   GROUP BY ROLLUP(Category, Year) ORDER BY 1,2, GROUPING_ID();
 Category    | Year |  SUM
-------------+------+--------
 Books       | 2005 |  39.98
 Books       | 2007 |  29.99
 Books       | 2008 |  29.99
 Books       |      |  99.96
 Electricity | 2005 | 109.99
 Electricity | 2006 | 109.99
 Electricity | 2007 | 229.98
 Electricity |      | 449.96
             |      | 549.92
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;example-using-rollup-with-the-having-clause&#34;&gt;Example: using ROLLUP with the HAVING clause&lt;/h2&gt;
&lt;p&gt;This example shows how to use the &lt;a href=&#34;../../../../../en/sql-reference/statements/select/having-clause/&#34;&gt;HAVING&lt;/a&gt; clause with &lt;code&gt;ROLLUP&lt;/code&gt; to restrict the &lt;code&gt;GROUP BY&lt;/code&gt; results. The following query produces only those &lt;code&gt;ROLLUP&lt;/code&gt; categories where &lt;code&gt;year&lt;/code&gt; is subtotaled, based on the expression in the &lt;code&gt;GROUPING&lt;/code&gt; function:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT Category, Year, SUM(Amount) FROM expenses
   GROUP BY ROLLUP(Category,Year) HAVING GROUPING(Year)=1
   ORDER BY 1, 2, GROUPING_ID();
 Category    | Year |  SUM
-------------+------+--------
 Books       |      |  99.96
 Electricity |      | 449.96
             |      | 549.92
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The next example rolls up on (&lt;code&gt;Category&lt;/code&gt;, &lt;code&gt;Year&lt;/code&gt;), but not on the full results. The &lt;code&gt;GROUPING_ID&lt;/code&gt; function specifies to aggregate less than three levels:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT Category, Year, SUM(Amount) FROM expenses
   GROUP BY ROLLUP(Category,Year) HAVING GROUPING_ID(Category,Year)&amp;lt;3
   ORDER BY 1, 2, GROUPING_ID();
 Category    | Year |  SUM
-------------+------+--------
 Books       | 2005 |  39.98
 Books       | 2007 |  29.99
 Books       | 2008 |  29.99
 Books       |      |  99.96
 Electricity | 2005 | 109.99
 Electricity | 2006 | 109.99
 Electricity | 2007 | 229.98
 Electricity |      | 449.96
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;see-also&#34;&gt;See also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/data-analysis/data-aggregation/#&#34;&gt;Data aggregation&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/statements/select/group-by-clause/cube-aggregate/#&#34;&gt;CUBE aggregate&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/grouping/#&#34;&gt;GROUPING&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/group-id/#&#34;&gt;GROUP_ID&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/aggregate-functions/grouping-id/#&#34;&gt;GROUPING_ID&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/statements/select/group-by-clause/#&#34;&gt;GROUP BY clause&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/statements/select/group-by-clause/grouping-sets-aggregate/#&#34;&gt;GROUPING SETS aggregate&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
  </channel>
</rss>
