title: Grouping sets columns in aggregate arguments and NULL replacement
body: <p>There are many grouping sets examples on the internet like query Q1 in the example below.  But query Q2 is different because A2 is a grouping column and it is used as the argument to SUM().</p>
  
  <p>Which one of the following is correct for Q2 according to the SQL Standard (any version <del>since 2003</del> that supports grouping sets)?  If (1) is correct, please explain why with reference to the Standard.</p>
  
  <ol>
  <li><p>A2 is replaced by NULL unless it is in an argument to an aggregate.  This interpretation would give results R1 below.  This is Oracle's behaviour (which seems more useful).</p></li>
  <li><p>A2 is replaced by NULL including where it is used in an aggregate: this means that the aggregate will return NULL.
  This interpretation would give results R2 below.  This is how I have understood the SQL Standard (possibly incorrectly).</p></li>
  </ol>
  
  <p>Example code:</p>
  
  <pre><code>-- Setup
  create table A (A1 int, A2 int, A3 int);
  insert into A values (1, 1, 100);
  insert into A values (1, 2, 40);
  insert into A values (2, 1, 70);
  insert into A values (5, 1, 90);
  
  -- Query Q1
  -- Expected/Observed results:
  --
  --         A1         A2    SUM(A3)
  -- ---------- ---------- ----------
  --          1          -        140
  --          2          -         70
  --          5          -         90
  --          -          1        260
  --          -          2         40
  --          -          -        300
  select A1, A2, sum (A3)
  from A
  group by grouping sets ((A1), (A2), ())
  order by 1, 2;
  
  -- Query Q2
  -- Results R1 (Oracle):
  --         A1         A2    SUM(A2)
  -- ---------- ---------- ----------
  --          1          -          3
  --          2          -          1
  --          5          -          1
  --          -          1          3
  --          -          2          2
  --          -          -          5
  -- 
  -- Results R2 (SQL Standard?):
  --         A1         A2    SUM(A2)
  -- ---------- ---------- ----------
  --          1          -          - 
  --          2          -          - 
  --          5          -          - 
  --          -          1          3
  --          -          2          2
  --          -          -          -   -- NULL row
  select A1, A2, sum (A2)
  from A
  group by grouping sets ((A1), (A2), ())
  order by 1, 2;
  </code></pre>
  
  <p>I am aware of this from SQL 2003 7.9 Syntax 17, which describes how columns are replaced with NULLs.  However, I might have missed or misunderstood a rule elsewhere that excludes arguments to aggregates.</p>
  
  <pre class="lang-none prettyprint-override"><code>m) For each GS_i:
     iii) Case:
          1) If GS_i is an &lt;ordinary grouping set&gt;, then
             A) Transform SL2 to obtain SL3, and transform HC to obtain
                HC3, as follows:
                II) Replace each &lt;column reference&gt; in SL2 and HC that
                    references PC_k by "CAST(NULL AS DTPCk)"
  </code></pre>