Sql-server – Performing Sum in Case Statement

sql serversql-server-2016t-sql

I am trying to get a sum inside a case statement, but I am getting the error

Cannot perform an aggregate function on an expression containing an aggregate or subquery

This is my query syntax

Select case
          when sc.ars = 0 then 0
          else SUM(sc.ars / NULLIF(SUM(ic.lit),0))
       end
FROM salesconfig sc
Left Join income ic
On sc.empID = ic.empID

How do I need to change this query so it gives me the result and not an error?

Best Answer

Test

Select case when sc.ars = 0 
            then 0
            else SUM(sc.ars / NULLIF(SUM(ic.lit) OVER (PARTITION BY NULL),0))
            end
FROM salesconfig sc
Left Join income ic
On sc.empID = ic.empID
Related Question