我有一个在Microsoft Access环境中工作的UNION查询。我在SQL Server中得到的错误是:“每个GROUP BY表达式必须包含至少一个不是外部引用的列”。查询格式如下:
SELECT tblA.ProjectID,
tblB.PersonnelID,
"TeamMember" AS ProjectRole
FROM tblA INNER JOIN tblB ON (tblA.ProjectID = tblB.ProjectID)
AND (tblA.ProjectID = tblB.ProjectID)
GROUP BY tblA.ProjectID, tblB.PersonnelID, "TeamMember"
HAVING ((Not (tblB.PersonnelID) Is Null) AND ((Sum(tblB.Hours))>0))
如何使此查询适用于SQL Server?
答案 0 :(得分:0)
afaik having
仅适用于像sum这样的aggragate函数,所以我将not null
内容移到where
中。引号也需要单打。我还将连接标准放在一个括号中。试试这个:
SELECT tblA.ProjectID,
tblB.PersonnelID,
'TeamMember' AS ProjectRole
FROM tblA
INNER JOIN
tblB
ON (tblA.ProjectID = tblB.ProjectID
AND tblA.ProjectID = tblB.ProjectID)
where tblB.PersonnelID is not null
GROUP BY
tblA.ProjectID
, tblB.PersonnelID
, 'TeamMember'
HAVING Sum(tblB.Hours) > 0