最終試験では、かなり厄介な SQL、リレーショナル代数、リレーショナル微積分クエリを取得しました。私はこのクエリを得ました:
カテゴリ「コンピュータ」のすべての製品を注文したクライアントの名前を見つけます。(パソコンカテゴリー全商品ご注文のお客様)
スキーマは次のとおりです。
顧客 ( Customer_Id、Cust_First_Name、Cust_Last_Name)
注文 ( Order_Id , *Customer_Id*)
Order_items ( Order_Item_id , *Order_Id*, *Product_Id*)
Product_info ( Product_Id、 Product_Name 、 Category)
ボールド (主キー)、イタリック (外部キー)
このクエリをリレーショナル代数に変換するには、サブクエリではなく結合を使用する必要があります。少し自分を助けるために、まず SQL を書き、次に SQL クエリをリレーショナル代数に変換します。
ここに私の試みがあります:
1を試してください(サブクエリを使用):
select C.Customer_Id
from Customer C
where
(
select count(*)
from product_info
where category = 'Computer'
)=(select count (distinct pi.Product_id)
from orders S, order_items OI, product_info pi
where S.Customer_Id = C.Customer_Id and S.Order_Id = OI.Order_Id and pi.product_id=OI.product_id and category = 'Computer')
2 を試してください (having 句で 1 つのサブクエリを使用):
select C.Customer_Id
from Customer C, Product_info pi, Orders S, Order_Items oi
where C.Customer_Id = S.Customer_Id and S.Order_Id = OI.Order_Id and OI.Product_Id = pi.Product_Id and pi.category = 'Computer'
group by C.Customer_Id
having count (distinct pi.Product_Id) =
(
select count (*)
from Product_info
where category = 'Computer'
)
3 を試してください (from 句のサブクエリ):
select C.Customer_Id
from Customer C, Product_info pi, Orders S, Order_Items oi,
(
select count (*) num
from Product_info
where category = 'Computer'
) numbr
where C.Customer_Id = S.Customer_Id and S.Order_Id = OI.Order_Id and OI.Product_Id = pi.Product_Id and pi.category = 'Computer'
group by C.Customer_Id, numbr.num
having count (distinct pi.Product_Id) = numbr.num
現在、このクエリは関係代数で表すことができますが、値が重複するため非効率的です。
私の最後の試み(これはコンパイルせず、どこでサブクエリを使用します):
select *
from Customer C
where not exists
(select *
from (select Order_Id from orders O where O.Customer_Id = C.Customer_Id) S INNER JOIN order_items OI on S.Order_Id = OI.Order_Id
RIGHT OUTER JOIN (select Product_Id from product_info where category ='Computer') PI on PI.Product_Id = OI.Product_Id
where OI.Product_Id = null)
この場合、LATERALを使用できるとどこかで読みましたが、LATERALに関する情報がほとんどないため、正しく理解できませんでした。
試験は終わりましたが、解決策についてはまだ興味があります。この 6 つのクエリ、ER ダイアグラム、ER-To-Relational、BCNF への正規化、3NF を含む 2 時間の試験だったので、このクエリを解決するのがいかに難しいかが頭に浮かびます。ここで重要な何かが欠けていますか?
これは私を少し助ける小さなサンプルデータです:
前もって感謝します。