title: postgresql, named grouping sets?
body: <p>Is there a way to name grouping sets? For each grouping set (explicitly defined or generated with rollup or cube as per <a href="https://www.postgresql.org/docs/devel/static/queries-table-expressions.html" rel="nofollow noreferrer">https://www.postgresql.org/docs/devel/static/queries-table-expressions.html</a>), I'd like to somehow specify a name in a result column. Here is an ugly instant demo of what I'm trying to do, with the name just being a list of the grouped columns:</p>
  
  <pre><code>select *, array_to_string(array_remove(array[case when "A" is null then null else 'A' end,
                                               case when "B" is null then null else 'B' end,
                                               case when "C" is null then null else 'C' end
                                              ],null),',') as grouping_set
  from (values  ('a','b','c'),
                ('aa','bb','cc'),
                ('aaa',null,'ccc')) as foo("A","B","C")
  group by rollup(1,2,3);
  
  
    A  | B  |  C  | grouping_set
  -----+----+-----+--------------
   a   | b  | c   | A,B,C
   a   | b  |     | A,B
   a   |    |     | A
   aa  | bb | cc  | A,B,C
   aa  | bb |     | A,B
   aa  |    |     | A
   aaa |    | ccc | A,C   &lt;--------- should be A,B,C
   aaa |    |     | A     &lt;--------- should be A,B
   aaa |    |     | A 
       |    |     |
  </code></pre>
  
  <p>But notice there's a problem with two of the rows, flagged with the arrows: both of those include column B in the grouping but not in the name because B is null in those groups. </p>
  
  <p>Any ideas or better ways of going about this?</p>