我使用多个表中的信息来尝试获取输出,供应商ID,供应商名称,带跟踪号的订单百分比。
我需要来自许多表的信息,如下所示:
到目前为止我所拥有的是:
SELECT DISTINCT so.supplierID, count(*) AS NumberOfOrders FROM SalesOrders so
INNER JOIN suppliers s ON s.SupplierID = so.SupplierID
INNER JOIN SupplierSubscriptions ss ON s.SupplierID = ss.SupplierID
INNER JOIN shipments ship ON ship.SalesOrderID = so.SalesOrderID
INNER JOIN Packages p ON p.ShipmentID = ship.ShipmentID
WHERE ss.SubscriptionID = 91 GROUP BY so.SupplierID
显然,这不是我所追求的,因为它只显示供应商ID和订单数量......
答案 0 :(得分:0)
我猜测架构的某些部分,但是这样的事情对你有用:
SELECT so.supplierID,
so.supplierName,
SUM(case when p.trackingNumber is not null then 1 else 0 end) / COUNT(0) as OrdersWithTracking
FROM SalesOrders so
INNER JOIN suppliers s ON s.SupplierID = so.SupplierID
INNER JOIN SupplierSubscriptions ss ON s.SupplierID = ss.SupplierID
INNER JOIN shipments ship ON ship.SalesOrderID = so.SalesOrderID
INNER JOIN Packages p ON p.ShipmentID = ship.ShipmentID
WHERE ss.SubscriptionID = 91
GROUP BY so.SupplierID, so.supplierName