0

我有一个数据库,其中包括一个分配表和一个员工表。每个表都包含员工 ID。多个员工在同一个项目上工作,我需要能够获得每个项目 ID 和工作时间的员工姓名列表。我尝试了以下方法,但无法正常工作。

Select FirstName, LastName, ProjectID, HoursWorked
From employee e, assignment a
Where e.EmployeeNumber = a.EmployeeNumber
Group by projectID; 

编辑:在第一个答案之后,我将查询更改为:

SELECT  FirstName, LastName, ProjectID, HoursWorked
FROM    employee e, assignment a
Where e.EmployeeNumber = a.EmployeeNumber
GROUP   BY ProjectID, FirstName, LastName;

结果很好,我想我不完全理解分组。

4

1 回答 1

1

您需要使用聚合函数 SUM()来获取列的总值,

SELECT  FirstName, 
        LastName, 
        ProjectID, 
        SUM(HoursWorked) totalHoursWorked
FROM    employee e
        INNER JOIN assignment a
            ON e.EmployeeNumber = a.EmployeeNumber
GROUP   BY FirstName, LastName, ProjectID
于 2013-03-09T22:11:03.780 回答