1

テキサス州のすべての店舗から商品を購入したすべての顧客を一覧表示するクエリを作成しようとしています。

したがって、ここに 4 つのテーブルとその属性があります。

Customers:
customerID, CustomerName 

Purchases:
CustomerID, ProductID 

StoresInventory:
StoreID, ProductID

StoreLocation:
StoreID, State

テキサス州のすべての店舗を返すクエリを作成できますが、顧客がこれらすべての店舗から商品を購入したかどうかを確認する方法がわかりません。また、countまたはは使用できませんaggregation。誰でもこれを行う方法を知っていますか?

4

3 に答える 3

2

次のようなことを試してください:

Select c.CustomerId From Customers c
Inner Join Purchases p on C.CustomerId=p.CustomerId
Inner Join StoresInventory s on s.ProductID=p.ProductID
Group By c.CustomerId
Having COUNT(Distinct s.StoreId)=(Select Count(Distinct StoreId) From StoreLocation Where State='Texas')
于 2013-03-20T04:45:55.020 に答える
1

ProductID がストアに固有のものでない限り。必要なデータがスキーマに格納されていません。顧客がどの製品を購入し、どのストアがそれらの製品を在庫しているかはわかっていますが、顧客がどのストアから製品を購入したかはわかりません。

于 2013-03-20T04:36:00.923 に答える
0
SELECT a.customerid
FROM purchases a
JOIN store_inventory b
    ON a.productid=b.productid
GROUP BY  a.customerid
HAVING count(distinct storeid)= 
    (SELECT count(*)
    FROM store_location s);
于 2018-11-26T06:30:49.080 に答える