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 46B569FA2EA for ; Thu, 14 Sep 2006 09:02:37 -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 52490-01 for ; Thu, 14 Sep 2006 12:02:27 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey- Received: from talk.nabble.com (www.nabble.com [72.21.53.35]) by postgresql.org (Postfix) with ESMTP id 71DD29FB1E0 for ; Thu, 14 Sep 2006 09:02:27 -0300 (ADT) Received: from [72.21.53.38] (helo=jubjub.nabble.com) by talk.nabble.com with esmtp (Exim 4.50) id 1GNpvF-0005EB-H2 for pgsql-sql@postgresql.org; Thu, 14 Sep 2006 05:02:25 -0700 Message-ID: <6304397.post@talk.nabble.com> Date: Thu, 14 Sep 2006 05:02:25 -0700 (PDT) From: zqzuk To: pgsql-sql@postgresql.org Subject: nested select within a DISTINCT block MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Nabble-From: ziqi.zhang@hotmail.com X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.16 tagged_above=0 required=5 tests=FORGED_HOTMAIL_RCVD2, SPF_HELO_PASS, SPF_PASS X-Spam-Level: * X-Archive-Number: 200609/113 X-Sequence-Number: 26324 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 package_id, #of bookings 1 0 here are what i tried select distinct b.product_package_id, count (distinct b.customer_id and not exists (select cc from cancellation cc where cc.booking_id=b.booking_id)) from booking as b group by b.product_package_id and it doesnt work. the syntax within the DISTINCT is wrong, unsurprisingly. i also tried select distinct b.product_package_id, count (distinct b.customer_id not in (select cc from cancellation cc where cc.booking_id=b.booking_id)) from booking as b group by b.product_package_id it produced incorrect result. ie, for those canceled bookings are also counted, producing package_id, #of bookings 1 3 which supposed to be package_id, #of bookings 1 0 could anyone give any hints please, many thanks ! -- View this message in context: http://www.nabble.com/nested-select-within-a-DISTINCT-block-tf2271181.html#a6304397 Sent from the PostgreSQL - sql forum at Nabble.com.