当前,我正在尝试从两个单独的表中查询特定的列。为此,我选择在SELECT
子句中使用FROM
语句。
这是我尝试的以下查询:
SELECT Employees.Last_Name, Employees.First_Name, Employees.PositionID FROM
Employees,
(SELECT Positions.PositionID, SUM(Positions.Position_Salary) AS Salary FROM
Positions GROUP BY PositionID) AS Subquery
WHERE Employees.PositionID = Subquery.PositionID
我们可以看到,我正在尝试根据每个记录各自的PositionID
用Salary
的求和列对它们进行分组。不幸的是,Salary
列没有出现。
观察:
在随后的测试中,我删除了Employee.First_Name,Employee.Last_Name,但是Salary的求和列仍未出现。
有什么想法吗?
答案 0 :(得分:2)
您需要将Salary
列添加到SELECT
子句中:
SELECT Employees.Last_Name, Employees.First_Name, Employees.PositionID,
Subquery.Salary -- added it here
FROM Employees,
(SELECT Positions.PositionID, SUM(Positions.Position_Salary) AS Salary FROM
Positions GROUP BY PositionID) AS Subquery
WHERE Employees.PositionID = Subquery.PositionID
答案 1 :(得分:1)
如果我了解您想要每个员工的总薪金:
SELECT
Employees.Last_Name,
Employees.First_Name,
Employees.PositionID,
(SELECT SUM(Positions.Position_Salary)
FROM Positions
WHERE Employees.PositionID = Positions.PositionID) AS TotalSalary
FROM Employees