X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (wm.hub.org [200.46.204.128]) by postgresql.org (Postfix) with ESMTP id 7DA289FB3DD for ; Fri, 22 Sep 2006 15:03:34 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.128]) (amavisd-new, port 10024) with ESMTP id 70147-08-2 for ; Fri, 22 Sep 2006 18:01:29 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from wolff.to (wolff.to [66.93.197.194]) by postgresql.org (Postfix) with SMTP id 682759FB477 for ; Fri, 22 Sep 2006 14:19:02 -0300 (ADT) Received: (qmail 3896 invoked by uid 500); 22 Sep 2006 17:28:41 -0000 Date: Fri, 22 Sep 2006 12:28:41 -0500 From: Bruno Wolff III To: zqzuk Cc: pgsql-sql@postgresql.org Subject: Re: nested select within a DISTINCT block Message-ID: <20060922172841.GB3193@wolff.to> Mail-Followup-To: Bruno Wolff III , zqzuk , pgsql-sql@postgresql.org References: <6304397.post@talk.nabble.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6304397.post@talk.nabble.com> User-Agent: Mutt/1.4.2.1i X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200609/156 X-Sequence-Number: 26367 On Thu, Sep 14, 2006 at 05:02:25 -0700, zqzuk wrote: > > Hi, here i have a problem with this task... > > I have a table "cancellation" which stores cancelled bookings and details of > charges etc > and a table "bookings" which stores details of bookings, for example: > > cancellation(cancellation_id, booking_id, charge) > booking(booking_id, customer_id, product_package_id, details) > > in the query, i wish to find, how many customers have booked for each > product_package_id. if there were 3 bookings for product_package_id=1, and > all these are cancelled and therefore exist in cancellation, then the query > result shoud display something like > > could anyone give any hints please, many thanks ! Left join booking and cancellation, select rows where cancellation_id IS NULL, GROUP BY either booking_id, and return that booking_id and count(*).