我有以下查询:
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
我希望很清楚,我所做的一切都会导致意外和不准确的数据。我认为我需要与小组有关,但每次尝试时,结果都是奇怪的。
编辑:将“count”混淆为“sum”
答案 0 :(得分: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
答案 1 :(得分: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