Lost_In_The_Sauce Lost_In_The_Sauce - 2 months ago 30
SQL Question

SQL using sum(days)

My question is in regards to using a function sum(days) and reusing it within my query.

select new_client, sum(days) as effort
from actuals
where week >='01/01/2016' and type is null and sum(days) = 0
group by new_client
order by effort desc


I know this doesn't work. I was wondering how one would do the above.

Thanks!

Answer
select new_client, sum(days) as effort
from actuals
where week >='01/01/2016' and type is null                          
group by new_client
having sum(days) = 0
order by effort desc

Something like this should work? You can't use SUM() in your where clause. That's why HAVING exists.

Question is why you would want that SUM(days) in your select then, because it would always show 0 if there are results.