#  Other Applications & Softwares  > Access Tables & Databases >  > [SOLVED] SQL to return the equivalent of SUMIF on two other columns (probably easy)

## mc84excel

This is probably easy to do but it usually is when you know how! (I'm still learning SQL)


Say I have a query that returns detailed results for three fields. The first two fields are categories and the third field is a quantity. I want to return the sum of the quantity for each combination of the two categories.

To make the example more practical, say the first two categories are UserName & Task and the quantity is Hours. How do write my SQL query so that I get the total Hours spent per UserName & Task?

----------


## AB33

Like you, I am also just started to learn SQL, so my suggestion may not make sense or work, but it seems to me that you need  the following statements:
SELECT  sum(quantity)
FROM Tablename
WHERE to filter the rows
GROUP BY
HAVING
ORDER BY

----------


## Norie

It would be an aggregrate query where you would group by the categories and sum the quantity.

Here's some simplified SQL.

SELECT UserName, Task, Sum(Hours) As TotalHours
FROM Table1
GROUP BY UserName, Task

----------


## mc84excel

Thanks AB33 & Norie. +1

(I ran with Nories solution. It didn't work first time I tried it then I realised I forgot to SUM the hours. Doh  :Roll Eyes (Sarcastic): )

----------

