nested select within a DISCTINCT block

Поиск
Список
Период
Сортировка
От zqzuk
Тема nested select within a DISCTINCT block
Дата
Msg-id 6310575.post@talk.nabble.com
обсуждение исходный текст
Ответы Re: nested select within a DISCTINCT block
Список pgsql-sql
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.



В списке pgsql-sql по дате отправления:

Предыдущее
От: Reinoud van Leeuwen
Дата:
Сообщение: Re: How to delete multiple records
Следующее
От: Daryl Richter
Дата:
Сообщение: Re: nested select within a DISCTINCT block