Yop伙计们,我有这个小问题需要解决;我有两个由CustomerID链接的表[Customers]和[Orders]。每个客户都可以轻松获得订单的COUNT()......
SELECT c.CompanyName, COUNT(*) AS [Nbr of Orders]
FROM [Orders] o
INNER JOIN [Customers] c
ON c.CustomerID = o.CustomerID
GROUP BY c.CompanyName
但问题来自问题的最后部分:我需要获得该列表,但只有那些订单多于其中一个客户的订单(c.CompagnyName =' XYZ') 我试图添加类似这个WHERE子句
的东西WHERE COUNT(*) > (SELECT COUNT(*)
FROM [Orders] o
INNER JOIN [Customers] c
ON c.CustomerID = o.CustomerID
WHERE c.CompagnyName = 'XYZ')
SQL错误:P 我知道这看起来很荒谬,但如果有人能告诉我我错过的东西!
答案 0 :(得分:1)
看起来你可以使用having子句。所以你的榜样就是
SELECT c.CompanyName, COUNT(*) AS [Nbr of Orders]
FROM [Orders] o
INNER JOIN [Customers] c
ON c.CustomerID = o.CustomerID
GROUP BY c.CompanyName
having count(*) > 1
答案 1 :(得分:0)
我认为您需要一个HAVING子句而不是WHERE子句,因为您要对聚合函数进行过滤