I have three tables:
table 'received'
------------------
partner_id int
item_id int
table 'delivered'
------------------
item_id int
delivery_date date
customer_id int
table 'partners'
------------------
id int
name text
table 'customers'
------------------
id int
name text
What I'd like to query is which items have been delivered by which partners in a single delivery to a customer. Sometimes different partners deliver the same items, which should be filtered out as the delivered items don't contain dupes.
What I've come up with is this:
select
partner_id,
count(distinct item_id)
from
received
where item_id in
(select distinct item_id from delivered where delivery_date = '2010-07-14' and customer_id = 1)
group by partner_id;
Yet this gives me all delivered items including the dupes the partners have delivered. I have been thinking about this for a long time now, and have tried sub-selects using 'except', 'having' and others, but haven't gotten to a point that took me further.
I'd be greatful for any hints into the right direction. Thank you.
-- Edit --
Here's some sample data:
table 'received'
partner_id | item_id
-----------|---------
1 | 1
1 | 2
2 | 1
2 | 3
table 'delivered'
item_id | delivery_date | customer_id
--------|---------------|------------
1 | 2010-07-14 | 1
2 | 2010-07-14 | 1
3 | 2010-07-14 | 1
The current output is:
partner | amount
--------|------
1 | 2
2 | 2
The desired output is:
partner | amount
--------|------
1 | 2
2 | 1
Since the partner with ID 2 has delivered an item that was already delivered by partner 1.