我期望看到每個供應商已與每個客戶(間接通過分銷商)完成的總美元業務的細分,其中我試圖不使用語法Inner Join
。我基本上不了解由如下所示的兩個查詢所產生的兩個輸出端之間的差:嘗試瞭解查詢中的NULL運算符
查詢1
select customers.cust_id, vendors.vend_id, sum(OrderItems.item_price*OrderItems.quantity) as total_business from
(((Vendors left outer join products
on vendors.vend_id = products.prod_id)
left outer join OrderItems
on products.prod_id = OrderItems.prod_id)
left outer join Orders
on OrderItems.order_num = Orders.order_num)
left outer join Customers
on Orders.cust_id = Customers.cust_id
group by Customers.cust_id, vendors.vend_id
order by total_business
我得到以下輸出:
QUERY2
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
我不明白如何只有NULL
cust_id
的第一個輸出關聯在第二個輸出時,我們得到一些非空cust_id
s。爲什麼不第一輸出包括這些非NULL cust_id
的
謝謝