Question : Fixing - Cannot perform an aggregate function on an expression containing an aggregate

Recently I asked a question and received a correct answer for what I posted: http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_25019670.html#a26177825

The problem is that I simplified my query for ease of discussion but it appears that I need more help. Within each of my SUMs I need to be able to calculate a value based on another aggregate.

I am using a timesheet table with the fields of userid, date, hours and category.

I want to do a query similar to the following below. The key this time is that I need to be able to divide the sum by the count of distinct timesheets that have been turned in for a particular month:

SELECT Month(date),Year(Date),
       SUM(case when category = 1 then Hours/(Count(distinct userid) else 0 end) as Total1,
       SUM(case when category = 2 then Hours/(Count(distinct userid) else 0 end) as Total2
  FROM timesheet
 GROUP BY Month(date),Year(Date)

Answer : Fixing - Cannot perform an aggregate function on an expression containing an aggregate

try this

SELECT Month(date),Year(Date),
       SUM(case when category = 1 then Hours else 0 end) / Count(distinct userid) as Total1,
       SUM(case when category = 2 then Hours else 0 end)/ Count(distinct userid) as Total2
  FROM timesheet
 GROUP BY Month(date),Year(Date)
Random Solutions  
 
programming4us programming4us