CREATE TEMP TABLE products (product_id int, name text, price numeric)
CREATE TEMP TABLE sales (product_id int, units int);
ALTER TABLE products ADD PRIMARY KEY (product_id);
SELECT product_id, p.name, (sum(s.units) * p.price) AS sales FROM products p LEFT JOIN sales s USING (product_id) GROUP BY product_id;
ERROR: column "l.a_2" must appear in the GROUP BY clause or be used in an aggregate function
With the following commit
commit e49ae8d3bc588294d07ce1a1272b31718cfca5ef
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sat Aug 7 02:44:09 2010 +0000
We allow other columns from the table with primary key in SELECT even when they are not part of the aggregate or GROUP BY clause. (This is fine since having product key in GROUP BY implies that there can be only one value for those columns.). But it seems PG can not decipher the same when the primary column is buried deeper in subqueries.
It's a PG bug, not XC bug. Will provide a patch to PG. Problem is we can't ship the query with primary key in GROUP BY.