我有一个Select查询,用于从SharePoint文档集中提取公司信息(名称,参考号,管理员,员工等)。由于该公司只允许2名员工,我的目标是只检查并选择那些拥有两名以上员工的公司。
我的SQL代码:
Select [Entity Name], [Ref No], [Group], [Partner], [Manager], [Staff]
[Covering Temp Access], [Expiry Date]
FROM [Table Name]
GROUP BY [Entity Name]
HAVING COUNT([Staffs])>2;
这会返回错误:
您的查询不包含指定表达式“Ref No”作为聚合函数的一部分。
答案 0 :(得分:1)
这是您的查询:
Select [Entity Name], [Ref No], [Group], [Partner], [Manager], [Staff],
[Covering Temp Access], [Expiry Date]
FROM [Table Name]
GROUP BY [Entity Name]
HAVING COUNT([Staffs]) > 2;
查看SELECT
。大多数列不在GROUP BY
子句中,并且不是聚合函数的参数。
也许你打算:
SELECT [Entity Name], COUNT(*)
FROM [Table Name]
GROUP BY [Entity Name]
HAVING COUNT([Staffs]) > 2;
如果您需要给定实体的所有信息,请使用join
或in
:
SELECT *
FROM [Table Name]
WHERE [Entity Name] IN (SELECT [Entity Name]
FROM [Table Name]
GROUP BY [Entity Name]
HAVING COUNT([Staffs]) > 2
);