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 9C1759FA2D4 for ; Thu, 14 Sep 2006 22:30:26 -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 16742-04 for ; Fri, 15 Sep 2006 01:30:16 +0000 (UTC) X-Greylist: delayed 00:05:24.558354 by SQLgrey- Received: from alimail.net (potatis.alimail.net [84.19.131.11]) by postgresql.org (Postfix) with ESMTP id 95A599FA0FF for ; Thu, 14 Sep 2006 22:30:16 -0300 (ADT) Received: from [192.168.1.10] ([84.19.128.181]) by alimail.net (alimail.net [10.1.8.71]) (MDaemon.PRO.v8.0.4.R) with ESMTP id md50000775738.msg for ; Fri, 15 Sep 2006 03:24:32 +0200 In-Reply-To: References: Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <88EB0B61-90B2-49D4-AAD5-D5BA7164D50D@tele2.se> Cc: zqzuk , Content-Transfer-Encoding: 7bit From: Niklas Johansson Subject: Re: nested select within a DISCTINCT block Date: Fri, 15 Sep 2006 03:24:29 +0200 To: Daryl Richter X-Mailer: Apple Mail (2.752.2) X-Spam-Processed: alimail.net, Fri, 15 Sep 2006 03:24:32 +0200 (not processed: message from trusted or authenticated source) X-MDRemoteIP: 84.19.128.181 X-Return-Path: spot@tele2.se X-MDaemon-Deliver-To: pgsql-sql@postgresql.org Reply-To: spot@tele2.se 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/101 X-Sequence-Number: 26312 On 14 sep 2006, at 23.58, Daryl Richter wrote: > create table booking(booking_id int, customer_id int, > product_package_id > int, details text); > > create table cancellation(cancellation_id int , booking_id int, charge > decimal); > > insert into booking values( 1, 1, 1, 'Cxl Booking 1' ); > insert into booking values( 2, 2, 1, 'Cxl Booking 2' ); > insert into booking values( 3, 2, 1, 'Ok Booking 3' ); > insert into booking values( 4, 3, 2, 'Cxl Booking 4' ); > > insert into cancellation values( 1, 1, 1.00 ); > insert into cancellation values( 2, 2, 1.00 ); > insert into cancellation values( 3, 4, 1.00 ); > > > select distinct product_package_id, > ( select count(booking_id) > from booking b2 > where > b2.product_package_id = b1.product_package_id > and not exists ( select 1 from cancellation c where > c.booking_id = > b2.booking_id ) ) as uncancelled_bookings > from booking b1 > order by product_package_id; > > product_package_id uncancelled_bookings > --------------------- ----------------------- > 1 1 > 2 0 Given the above, you could also phrase it a little more natural, as follows: SELECT product_package_id, COUNT(b.booking_id)-COUNT(c.booking_id) AS un_cancelled_bookings FROM booking b LEFT JOIN cancellation c USING(booking_id) GROUP BY product_package_id ORDER BY product_package_id; I don't know about the amount and distribution of data in this case, but I think this will also give you a slightly better plan in most cases. Sincerely, Niklas Johansson