我尝试从某位老师那里获得课程提供的总持续时间,以及从该位老师的家乡获得的课程提供的总持续时间。我使用下一个查询:
SELECT SUM(dur) AS 'Totaal', (SELECT SUM(dur)
FROM dept d INNER JOIN emp e
ON e.deptno = d.deptno
WHERE d.loc = o.loc
GROUP BY trainer) AS 'In eigen stad'
FROM offr o INNER JOIN crs c
ON o.course = c.code
GROUP BY trainer, o.loc
问题在于,它给出了下一个错误消息:“每个GROUP BY表达式必须至少包含一个不是外部引用的列。”
知道为什么吗?
更新:
SELECT SUM(dur) AS Totaal, (SELECT SUM(dur)
WHERE o.trainer = e.empno
AND d.loc = o.loc) AS [In eigen stad]
FROM offr o INNER JOIN crs c
ON o.course = c.code
INNER JOIN dept d
ON o.loc = d.loc
INNER JOIN emp e
ON o.trainer = e.empno
GROUP BY trainer, o.loc
预先感谢
答案 0 :(得分:0)
您不希望在子查询中使用group by
。您需要一个相关的子查询。没有样本数据和理想的结果,很难说出您到底要计算什么,但我认为是:
SELECT SUM(dur) AS Totaal,
(SELECT SUM(dur)
FROM dept d INNER JOIN
emp e
ON e.deptno = d.deptno
WHERE d.loc = o.loc
) AS [In eigen stad]
FROM offr o INNER JOIN
crs c
ON o.course = c.code
GROUP BY trainer, o.loc