查找从未向员工下过订单的客户

Find customers that never placed an order with an employee

我们从供应商 ID、类别名称和产品数量的查询输出中获得了以下详细信息,
所以 3 列。每个供应商都拥有特定类别的产品,并且对于每个
类别,第三列列出了该特定类别中的产品数量。
如何使用 table which returns -
编写查询 供应商 if,类别名称,prodcount*100/totalnumberofproducts
基本上它应该列出该类别中产品的百分比

给出每个类别的产品数量的初始查询是:

SELECT s.SupplierID, s.CompanyName AS Supplier, cat.CategoryName, 
    COUNT(*) AS CatProductCount
FROM suppliers AS s
JOIN products AS p ON s.SupplierID = p.SupplierID  
JOIN categories AS cat ON p.CategoryID = cat.CategoryID 
GROUP BY s.SupplierID, s.CompanyName, cat.CategoryName;

我的想法:
所以如果初始查询是S.

我们可以做到

select supplierId, sum(productCount) as total from S groupby supplierID  

这将告诉我们每个供应商的产品总数。
让这个查询被称为 S2

select S.supplierId, (S.productCount*100)/S2.total from S inner join S2 on S.supplierID=S2.supplierId 

我认为我的想法是正确的,但我使用的确切语法不起作用。

你可以使用这两种可能性

已加入table

SELECT s.SupplierID, s.CompanyName AS Supplier, cat.CategoryName, 
    COUNT(*) * 100/ total as percentage_count
FROM suppliers AS s
JOIN products AS p ON s.SupplierID = p.SupplierID  
JOIN categories AS cat ON p.CategoryID = cat.CategoryID 
JOIN (select supplierId, sum(productCount) as total from suppliers group by supplierID ) S1 ON S1.supplierId = S,suppliers
GROUP BY s.SupplierID, s.CompanyName, cat.CategoryName;

或作为子选择

SELECT s.SupplierID, s.CompanyName AS Supplier, cat.CategoryName, 
    COUNT(*) * 100 / (select sum(productCount)  from suppliers S1 WHERE S1.suppliers  = S.supplierID ) as percentage_count
FROM suppliers AS s
JOIN products AS p ON s.SupplierID = p.SupplierID  
JOIN categories AS cat ON p.CategoryID = cat.CategoryID 
GROUP BY s.SupplierID, s.CompanyName, cat.CategoryName;

你应该测试两者,看看哪个更快,我猜是第二个