2017-11-09 7 views
0

私はしばらくこのことに執着しています。それは実験室であり、最後の質問です。上記のクエリを組み合わせて、コンピュータのみを提供するサプライヤを見つけました。どんな助けもありがとうございます。トラックとコンピュータのみを提供するサプライヤーの名前

私はコンピュータとトラックを供給するサプライヤとそれ以外のものを探す必要があります。

+0

supp_IDは、カラムから来るん。私はTB_offersと仮定します –

+0

または下のブール値がANDである必要がありますAND –

+0

これは正しい –

答えて

0

は、これは、あなたが提供しているものを、ここでのソリューションですから、コンピュータとトラックが、

with dat 
as 
(
select 'Toronto' Supp_id,'Computer' thename union all 
select 'Toronto' Supp_id,'Truck' thename union all 
select 'Toronto' Supp_id,'Furniture' thename union all 
select 'Ottawa' Supp_id,'Computer' thename union all 
select 'Ottawa' Supp_id,'Truck' thename union all 
select 'Montana' Supp_id,'Furniture' thename union all 
select 'Idaho' Supp_id,'Computer' thename union all 
select 'John' Supp_id,'Truck' thename union all 
select 'John' Supp_id,'Furniture' thename union all 
select 'Boris' Supp_id,'Computer' thename union all 
select 'Boris' Supp_id,'Truck' thename union all 
select 'Harold' Supp_id,'Furniture' thename union all 
select 'Yelson' Supp_id,'Furniture' thename 
) 
select Supp_ID from dat where thename = 'Computer' 
intersect 
select Supp_ID from dat where thename = 'Truck' 
except 
select Supp_ID from dat where thename not in ('Truck','Computer') 
0

他には何を提供するサプライヤーを提供します。

SELECT tp.name, 
     ts.supp_ID 
FROM Tb_Supplier ts 
inner join tb_offers tb on tb.supp_id = ts.supp_id 
inner join tb_prodcut tp on tp.prod_id = tb.prod_id 
WHERE ts.supp_ID not in (select ts.supp_ID 
         from Tb_Supplier ts 
         inner join tb_offers tb on tb.supp_id = ts.supp_id 
         inner join tb_prodcut tp on tp.prod_id = tb.prod_id 
         where tp.product not in ('computers', 'truck') 
         ) 
+0

サプライヤがコンピュータとトラック以外の他のアイテムを供給しても、依頼者は依然としてサプライヤをピックアップしますが、これはOPが望んでいないものです。 – Eric

+0

説明のためにありがとう、私は今必要な結果を得るべきサブクエリを追加しました。 @エリック –

0

これは「リレーショナル部門」

グループ化を必要とし、集計を比較すると、例えば、データを介して単一のPASで「ONLY」必要とする多くの問題を解決します

select Supp_ID 
from yourdata 
group by Supp_id 
having sum(case when product_name not in ('Truck','Computer') then 1 else 0 end) = 0 
and min(product_name) = 'Computer' 
and max(product_name) = 'Truck' 
; 

他の製品は、SUMが存在する場合は()<> 0 であり、次いで、minおよびmaxは、その後2つの製品

代替等しくする必要があります。このバリアントは

select Supp_ID 
from yourdata 
group by Supp_id 
having sum(case when product_name not in ('Truck','Computer') then 1 else 0 end) = 0 
and count(distinct product_name) = 2 
; 

を考慮すべき製品が2つ以上ある場合は、変更が容易です。

参照:Divided We Stand: The SQL of Relational Division(ジョー・セルコ)

関連する問題