Re: Column Alias Not Allowed In Grouping Function

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Vivek Suraiya <vivek_suraiya(at)yahoo(dot)com>
Cc: PostgreSQL Community <pgsql-www(at)lists(dot)postgresql(dot)org>
Subject: Re: Column Alias Not Allowed In Grouping Function
Date: 2022-08-16 19:32:58
Message-ID: 1408578.1660678378@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-www

Vivek Suraiya <vivek_suraiya(at)yahoo(dot)com> writes:
> SELECT cust_name c , sales_person_name s , SUM(sale_amt) , GROUPING(c , s) grpFROM SALESGROUP BY CUBE(c,s)ORDER BY grp;
> will fail.

Yup.

> Why is it illegal to use the column aliases in the GROUPING function?

Because the inputs to GROUPING are (notionally, at least) computed before
the SELECT targetlist is.

I'm aware that we allow things like "SELECT x ... GROUP BY 1", but
that's a holdover from SQL92 that's not even legal per SQL99 and
later standards. We're not going to try to extend that into
modern constructs like GROUPING(); it would create a morass of
ambiguity.

regards, tom lane

In response to

Browse pgsql-www by date

  From Date Subject
Next Message 三和陽菜 2022-08-17 04:13:48 Request for changing our logo
Previous Message Vivek Suraiya 2022-08-16 13:32:59 Column Alias Not Allowed In Grouping Function