Received: from postgresql.org.org (webmail.postgresql.org [216.126.85.28]) by postgresql.org (8.11.3/8.11.1) with SMTP id f5CGECE31866 for ; Tue, 12 Jun 2001 12:14:12 -0400 (EDT) (envelope-from pgsql-sql-owner@postgresql.org) Received: from ns.efaber.net ([194.30.88.220]) by postgresql.org (8.11.3/8.11.1) with SMTP id f56EkRE65544 for ; Wed, 6 Jun 2001 10:46:27 -0400 (EDT) (envelope-from gaizka@efaber.net) Received: (qmail 24029 invoked by uid 630); 6 Jun 2001 14:48:03 -0000 Received: from localhost (sendmail-bs@127.0.0.1) by localhost with SMTP; 6 Jun 2001 14:48:03 -0000 Date: Wed, 6 Jun 2001 16:48:03 +0200 (CEST) From: Gaizka Villate To: Subject: Sub-SELECT uses un-GROUPed attribute: how to solve. Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Archive-Number: 200106/180 X-Sequence-Number: 3409 Hi all! I want to make a report of (let's say) object sales, and i'm getting the error: Sub-SELECT uses un-GROUPed. I think this will be better understand with an example: (this is a over-simplified one, but it gets the point). (See data model and data to fill it below.) If i have: SALES: ------------------- Object | Datetime ------------------- chair | 2001-01-15 chair | 2001-01-16 table | 2001-01-17 chair | 2001-02-15 table | 2001-02-16 I want to get something as follows: Object | Month | Sales | Total sales on that month ------------------------------------------ chair | 01 | 2 | 3 table | 01 | 1 | 3 chair | 02 | 1 | 2 table | 02 | 1 | 2 well, if i do: select object, to_char(sale_date, 'MM') as Month, count(*) as Sales from sales group by to_char(sale_date, 'MM'), object; object | month | sales --------+-------+------- chair | 01 | 2 table | 01 | 1 chair | 02 | 1 table | 02 | 1 It's Ok. If i do: select object, to_char(sale_date, 'MM') as Month, count(*) as Sales, (select count(*) from sales s2 where to_char(s2.sale_date, 'MM') = to_char(s1.sale_date, 'MM') ) as Total_Sales from sales s1 group by to_char(sale_date, 'MM'), object; ERROR: Sub-SELECT uses un-GROUPed attribute s1.sale_date from outer query I get that error. I can understand it, since in the subselect i'm using s1.sale_date that is not grouped yet. My question is: is there a way to get that result? I've though of using a function, so it woud be: select object, to_char(sale_date, 'MM') as Month, count(*) as Sales, total_for_month(sale_date, 'MM') as Total_Sales from sales s1 group by to_char(sale_date, 'MM'), object; But i'd like to do it without using functions because i would have to define a function for each different period i want to show results. (for example, to get year or quarter totals instead of months). I think this is a common query to report sales. Does anybody found a beautiful solution? Thanks for your attention. -- Gaizka Villate -- Data model for the example: create table sales ( object varchar(30), sale_date date ); copy sales from stdin using delimiters '|'; chair|2001-01-15 chair|2001-01-16 table|2001-01-17 chair|2001-02-15 table|2001-02-16 \. ;