X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 7F3B99FA621 for ; Wed, 19 Jul 2006 11:29:43 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 13821-01 for ; Wed, 19 Jul 2006 11:29:32 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from myemma.com (db1.e2ma.net [66.179.147.163]) by postgresql.org (Postfix) with ESMTP id 0D08D9FA47F for ; Wed, 19 Jul 2006 11:29:31 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by myemma.com (Postfix) with ESMTP id 7BD8023CD7; Wed, 19 Jul 2006 09:31:34 -0500 (CDT) Received: from myemma.com ([127.0.0.1]) by localhost (velma [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 14130-03; Wed, 19 Jul 2006 09:31:34 -0500 (CDT) Received: from [192.168.1.68] (65.107.156.170.ptr.us.xo.net [65.107.156.170]) by myemma.com (Postfix) with ESMTP id 95FCA23CD3; Wed, 19 Jul 2006 09:31:30 -0500 (CDT) Message-ID: <44BE41BE.2000902@myemma.com> Date: Wed, 19 Jul 2006 09:29:18 -0500 From: Erik Jones User-Agent: Thunderbird 1.5.0.4 (Windows/20060516) MIME-Version: 1.0 To: Phillip Smith Cc: pgsql-sql@postgresql.org Subject: Re: Table Join (Maybe?) References: <015b01c6ab05$d77f7b00$9b0014ac@ITPhil> In-Reply-To: <015b01c6ab05$d77f7b00$9b0014ac@ITPhil> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at myemma.com X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/249 X-Sequence-Number: 25823 Phillip Smith wrote: > > Hi again all, > > I have two tables: > > 1. Sales figures by date and customer. > > 2. Customer details – including their Geographic State > > I need to extract a report from the first table (I can do that!), and > in that report order by their State (I can do that too!), but I also > need a summary of all the customers in each state, below the end of > each state, and have a grand total at the bottom. > > Eg: > > Customer 1 State 1 $100.00 > > Customer 2 State 1 $100.00 > > State 1 $200.00 > > Customer 3 State 2 $100.00 > > Customer 4 State 2 $100.00 > > State 2 $200.00 > > Grand Total $400.00 > > Does anyone have any magic pointers for me? I’ve been playing with > SELECT INTO as 2 queries (the individual customers, then the summary > figures added to the temp table) but I end up with ROWS IN FIRST QUERY > * ROWS IN SECOND QUERY instead of them all sorted together nicely L > > Thanks all, > > -p > Well, two queries one for the individual totals and one for the summary totals is good (maybe a third for the grand total), but you should do a union of the two and then play with the order by and/or group by clauses (depending on the data) to get the ordering that you want. I can't even count the times I've spent banging my head against the proverbial wall (you do have a proverbial wall don't you?) trying to get these kinds of queries to work with joins, sub-queries, case statements, etc... only to come back to using union on simple, to-the-point queries. -- erik jones software development emma(r)