在 AdventureWorks 2008 中,还有哪些其他(更好的)编写此查询的方法:
SELECT DISTINCT o.CustomerID, c.CustomerType,
ISNULL(s.Name, p.FirstName + ' ' + p.LastName) AS Name
FROM Sales.SalesOrderHeader o
INNER JOIN Sales.Customer c
ON c.CustomerID = o.CustomerID
LEFT OUTER JOIN sales.Individual i
ON i.CustomerID = o.CustomerID
LEFT OUTER JOIN sales.Store s
ON s.CustomerID = o.CustomerID
LEFT OUTER JOIN Person.Contact p
ON p.ContactID = i.ContactID
ORDER BY CustomerID
最好具有支持其他客户类型的能力。
结果如下所示:
CustomerID CustomerType Name
697 S Brakes and Gears
698 S Western Bike Supplies
699 S Sensational Discount Store
700 S Underglaze and Finish Company
701 S Future Bikes
11000 I Jon Yang
11001 I Eugene Huang
11002 I Ruben Torres
11003 I Christy Zhu