2012-07-12 76 views
1

我有以下查詢:MySQL的總和小時的速度

SELECT num_hours, rate, st.tech_code 
FROM ost_hours ht 
LEFT JOIN ost_staff st ON ht.tech_code = st.tech_code 
LIMIT 0 , 30 

其結果如下:

num_hours rate   tech_code 
20   overtime_rate 2 
4   overtime_rate 1 
10   normal_rate  1 
1   overtime_rate 4 
1   overtime_rate 4 
2   normal_rate  4 
3   normal_rate  4 
1.5   normal_rate  6 
1.5   normal_rate  6 
2   normal_rate  4 
1   normal_rate  4 

我想這導致每率總和爲每個tech_code,所以例如用於高科技碼4和6這將是如下:

total_hours rate   tech_code 
8   normal_rate  4 
2   overtime_rate 4 
3   normal_rate  6 

我希望這是明確的,我所做的一切已經導致unexpect編輯和不準確的數據。我認爲我需要與團隊合作,但每次嘗試時都會產生奇怪的結果。

編輯:改變混亂 「計數」 爲 「總和」

回答

3
select sum(num_hours) as total_hours, 
    rate, 
    st.tech_code 
from ost_hours ht 
left join ost_staff st on ht.tech_code = st.tech_code 
group by rate, 
    st.tech_code 
+0

總和將導致組中num_hours的總和,這是不需要的,它需要計數 – 2012-07-12 17:50:14

+0

不,OP表示「計數」,但意味着「計數總和」,即該字段是一個計數,並且OP需要該字段的總和。 – Umbrella 2012-07-12 17:51:35

+0

是的,總和就是我所需要的。對困惑感到抱歉! – 2012-07-12 19:51:41

0

嘗試此::

SELECT SUM(num_hours) as num_hours, rate, st.tech_code 
FROM ost_hours ht 
LEFT JOIN ost_staff st ON ht.tech_code = st.tech_code 
group by rate 
+0

計數()???或總和() – 2012-07-12 17:46:38

+0

如果你需要你顯示的內容,請使用count,試試吧 – 2012-07-12 17:47:49

+0

這個問題似乎意味着需要一個總和。我知道他說數,但這個例子是一個總和。 – 2012-07-12 17:48:50