pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: zqzuk <ziqi.zhang@hotmail.com>
To: pgsql-sql@postgresql.org
Subject: nested select within a DISCTINCT block
Date: Thu, 14 Sep 2006 10:13:26 -0700 (PDT)
Message-ID: <6310575.post@talk.nabble.com> (raw)


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-DISCTINCT-block-tf2272951.html#a6310575
Sent from the PostgreSQL - sql forum at Nabble.com.




view thread (4+ messages)  latest in thread

Message-ID: <6310575.post@talk.nabble.com>
Permalink:  ../6310575.post@talk.nabble.com/
Also on:    postgresql.org/message-id/6310575.post@talk.nabble.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: ziqi.zhang@hotmail.com
  Subject: Re: nested select within a DISCTINCT block
  In-Reply-To: <6310575.post@talk.nabble.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox