2014-02-05 90 views
0

我在Access 2003中的以下數據:查詢到在同一時間計算日期和金額數

Emp_ID Name   WorkDays TotalOT 
------ ------------ --------- ------- 
6  Dobles  1/25/2014 0 
6  Dobles  1/27/2014 1.5 
6  Dobles  1/28/2014 5 
6  Dobles  1/29/2014 0 
6  Dobles  1/30/2014 1 
7  Alfaro  1/25/2014 4 
7  Alfaro  1/27/2014 3.5 
7  Alfaro  1/28/2014 0 
7  Alfaro  1/29/2014 1 
7  Alfaro  1/30/2014 5 
7  Alfaro  1/31/2014 0 

我試圖建立與看起來像這樣的一個結果查詢:

Emp_ID Name   WorkDays TotalOT 
------ ------------ --------- ------- 
6  Dobles  5   7.6 
7  Alfaro  6   13.5 

到目前爲止,這裏是我的代碼:

SELECT DISTINCTROW 
    (Employee_Tbl.EmployeeID) AS Emp_ID, 
    (Employee_Tbl.FullName) AS Name, 
    Sum(Attendance_Tbl.WorkDate) AS TotalWorkDays, 
    Sum(Attendance_Tbl.TotalOTHrs) AS TotalOT 
FROM 
    Attendance_Tbl 
    INNER JOIN 
    Employee_Tbl 
     ON Attendance_Tbl.EmployeeID = Employee_Tbl.EmployeeID 
WHERE (((Attendance_Tbl.WorkDate)>=#1/25/2014# 
    And (Attendance_Tbl.WorkDate)<=#1/31/2014#)) 
GROUP BY 
    Employee_Tbl.EmployeeID, 
    Employee_Tbl.FullName, 
    Attendance_Tbl.WorkDate, 
    Attendance_Tbl.TotalOTHrs; 

但是,讓我

Emp_ID Name TotalWorkDays TotalOT 
------ ------ ------------- ------- 
    6 Dobles   41664  0 
    6 Dobles   41666  1.5 
    6 Dobles   41667  5 
    6 Dobles   41668  0 
    6 Dobles   41669  1 
    7 Alfaro   41664  4 
    7 Alfaro   41666  3.5 
    7 Alfaro   41667  0 
    7 Alfaro   41668  1 
    7 Alfaro   41669  5 
    7 Alfaro   41670  0 

我在做什麼錯?

回答

2

你需要做三件事情:

  1. 刪除DISTINCTROW關鍵字。
  2. 將日期的Sum()函數更改爲Count()
  3. 從GROUP BY子句中刪除日期和OT字段。您只需要通過EmployeeID和名稱進行GROUP。

結果查詢將

SELECT 
    Employee_Tbl.EmployeeID AS Emp_ID, 
    Employee_Tbl.FullName AS Name, 
    Count(Attendance_Tbl.WorkDate) AS TotalWorkDays, 
    Sum(Attendance_Tbl.TotalOTHrs) AS TotalOT 
FROM 
    Attendance_Tbl 
    INNER JOIN 
    Employee_Tbl 
     ON Attendance_Tbl.EmployeeID = Employee_Tbl.EmployeeID 
WHERE (((Attendance_Tbl.WorkDate)>=#1/25/2014# 
    And (Attendance_Tbl.WorkDate)<=#1/31/2014#)) 
GROUP BY Employee_Tbl.EmployeeID, Employee_Tbl.FullName; 

返回

Emp_ID Name TotalWorkDays TotalOT 
------ ------ ------------- ------- 
    6 Dobles    5  7.5 
    7 Alfaro    6  13.5