0
我最初希望看到每個供應商與每個客戶(間接通過分銷商)完成的總美元業務的細目,我試圖不使用Inner Join Syntax並使用下面查詢這個目的:瞭解子查詢
select customers.cust_id, Vendors.vend_id, sum(quantity*item_price) as total_business from
(((Vendors left outer join Products
on Products.vend_id = Vendors.vend_id)
left outer join OrderItems --No inner joins allowed
on OrderItems.prod_id = Products.prod_id)
left outer join Orders
on Orders.order_num = OrderItems.order_num)
left outer join Customers
on Customers.cust_id = Orders.cust_id
where Customers.cust_id is not null -- THE ONLY DIFFERENCE BETWEEN QUERY1 AND QUERY2
group by Customers.cust_id, Vendors.vend_id
order by total_business
現在,我想看看所有供應商的客戶組合查詢的輸出結果,包括在沒有任何商業交易的組合,我試圖通過一個單一的SQL寫這查詢。我的老師提供了這個解決方案,但我真的無法理解這個邏輯,因爲我從來沒有遇到過子查詢。
select
customers.cust_id,
Vendors.vend_id,
sum(OrderItems.quantity*orderitems.item_price)
from
(
customers
inner join
Vendors on 1 = 1
)
left outer join --synthetic product using joins
(
orders
join
orderitems on orders.order_num = OrderItems.order_num
join
Products on orderitems.prod_id = products.prod_id
) on
Vendors.vend_id = Products.vend_id and
customers.cust_id = orders.cust_id
group by customers.cust_id, vendors.vend_id
order by customers.cust_id
非常感謝