You want a group by
statement, something like this:
SELECT t2.Customer, sum(t1.hours) as hours
FROM table1 t1 LEFT JOIN
table2 t2
ON t1.id = t2.id
WHERE t2.Customer is not null
GROUP BY t2.Customer
ORDER BY t2.Customer ASC;
Domanda
How can I get the sum of each customer's hours? My data looks like this
Customer | Hours
C1 | 15
C1 | 13
C3 | 23
C4 | 10
C4 | 5
Customer and Hours are in a separate tables. I did my query like this:
SELECT DISTINCT t2.Customer
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id
WHERE t2.Customer is not null
ORDER BY t2.Customer ASC
And the result of this is
Customer
C1
C3
C4
I want to do next is to sum the hours of the customers so the output would look like this:
Customer | Hours
C1 | 28
C3 | 23
C4 | 15
Soluzione
You want a group by
statement, something like this:
SELECT t2.Customer, sum(t1.hours) as hours
FROM table1 t1 LEFT JOIN
table2 t2
ON t1.id = t2.id
WHERE t2.Customer is not null
GROUP BY t2.Customer
ORDER BY t2.Customer ASC;