2012-07-25 151 views
1

比方說,我有如下的表: EmployeeID, PayEntity, HourWorked, Date其中PayEntity can be 1:Normal, 2:OverTime, etc我怎樣才能小時的平均工作了一段時間,例如,如果我們確實有我的表的數據:獲取平均此查詢

1, 1, 40, 7/25/12 
1, 2, 5, 7/25/12 
2, 1, 30, 7/25/12 

平均此日期必須(40+5)+30/2 -- 37.5

回答

3

嘗試使用你找到每天每個員工的總小時數子查詢和外選擇您計算出平均每天:

SELECT Date, AVG(HourWorked) AS HourWorked 
FROM 
(
    SELECT Date, SUM(HourWorked) AS HourWorked 
    FROM yourtable 
    GROUP BY EmployeeId, Date 
) T1 
GROUP BY Date 

看到它聯機工作:sqlfiddle

+0

由於它的工作原理正是我想要的方式。 – 2012-07-25 23:55:38

0

按照同樣的思路,但不知道有一個AVG功能

SELECT employee_id, total_hours, days, total_hours/days as average, 
FROM (
    SELECT employee_id, SUM(hours) as total_hours, COUNT(date) as days FROM `employee_hours_table` GROUP BY `employee_id` 
) data