title: Optimization of GROUP BY which contains the grouping rules with OR condition
body: <p>There is some table <code>T1</code> (in the Oracle database) with some fields <code>A</code>, <code>B</code>, <code>C</code>, <code>D</code>, <code>E</code>, <code>F</code>:</p>
  
  <p><strong>Upd 0:</strong> Let the types of the above fields be the same.</p>
  
  <p>Suppose, we need to group our table by the following rule:
  <code>A &amp; B &amp; (C | D)</code></p>
  
  <p><strong>Upd 1:</strong>
  The <code>A &amp; B &amp; (C | D)</code> expression can be transformed to the following expression: </p>
  
  <p><code>(A &amp; B &amp; C) | (A &amp; B &amp; D)</code>. </p>
  
  <p>Thus, to solve this task I have to union two grouping queries for groups <code>A, B, C</code> and <code>A, B, D</code>: </p>
  
  <pre><code>select A, B, C, count(*) 
  from T1 
  group by A, B, C
  
    union all
  
  select A, B, D, count(*) 
  from T1 
  group by A, B, D
  </code></pre>
  
  <p>If the grouping rule will be more complicated: <code>A &amp; B &amp; (C | D) &amp; (E | F)</code>, then the solution will be more bulky, because I have to union grouping queries for the following groups: </p>
  
  <p><code>A &amp; B &amp; C &amp; E</code>, <code>A &amp; B &amp; D &amp; E</code>, <code>A &amp; B &amp; C &amp; F</code>, <code>A &amp; B &amp; D &amp; F</code>.</p>
  
  <p>Is there any possibility to optimize such solution?
  Or may be there is a better way to solve such tasks?</p>
  
  <p><strong>Upd 2:</strong>
  I used short form of expressions <code>A &amp; B &amp; (C | D)</code> and <code>A &amp; B &amp; (C | D) &amp; (E | F)</code> to emphasize that they have a common part <code>A &amp; B</code>. And I don't want it to be calculated many times.</p>