有人愿意指出我正确的方向吗?我正在使用AdventureWorks2012数据库来完成一些培训,我不太清楚如何解决我需要找到所有不使用“OVERSEAS -DELUXE”或“过夜”运输方式的采购订单(SalesOrderHeader)的问题J-FAST“(ShipMethod)和我必须使用公用表表达式。
这是我到目前为止所提出的,这似乎是不正确的:
WITH filteredshipmethods (ShipMethodID, Name, PurchaseOrderNumber)
as
(SELECT Purchasing.PurchaseOrderHeader.ShipMethodID, Purchasing.ShipMethod.Name, Sales.SalesOrderHeader.PurchaseOrderNumber
FROM Purchasing.PurchaseOrderHeader INNER JOIN
Purchasing.ShipMethod ON Purchasing.PurchaseOrderHeader.ShipMethodID = Purchasing.ShipMethod.ShipMethodID INNER JOIN
Sales.SalesOrderHeader ON Purchasing.ShipMethod.ShipMethodID = Sales.SalesOrderHeader.ShipMethodID
WHERE Purchasing.ShipMethod.Name <> 'OVERSEAS - DELUXE' AND Purchasing.ShipMethod.Name <> 'OVERNIGHT J-FAST' And PurchaseOrderNumber is not null)
SELECT PurchaseOrderNumber, ShipMethodID, Name
From filteredshipmethods
Group By ShipMethodID, Name, PurchaseOrderNumber
Order By Name;
答案 0 :(得分:0)
如果任务只是找到SalesOrderHeaders,那你为什么要加入PurchaseOrderHeader?并且请使用别名,它将帮助您最小化代码并更好地理解您拥有的内容。
WITH filteredshipmethods
AS ( SELECT soh.PurchaseOrderNumber ,
sm.ShipMethodID ,
sm.Name
FROM Sales.SalesOrderHeader soh
INNER JOIN Purchasing.ShipMethod sm ON soh.ShipMethodID = sm.ShipMethodID
WHERE sm.Name <> 'OVERSEAS - DELUXE'
AND sm.Name <> 'OVERNIGHT J-FAST'
)
SELECT PurchaseOrderNumber ,
ShipMethodID ,
Name
FROM filteredshipmethods
GROUP BY PurchaseOrderNumber ,
ShipMethodID ,
Name