I am trying to write a query against a table that contains daily payroll data for employees at different locations. I need to be able to get a count of employee ids that have greater than 30 paid hours in a week and a count of employee ids that have less than 20 for the same week. I've tried this with no luck:
select
[WEEKENDING],
[LOCATION],
sum(case when [PAID_HOURS] > 30 then 1 else 0 end) as [COUNT_OVER_30],
sum(case when [PAID_HOURS] < 20 then 1 else 0 end) as [COUNT_UNDER_20]
from mydb.payroll
group by [WEEKENDING],[LOCATION]
This is the source table:
Here is the desired result:
Thank you so much in advance!!!


You seem to have the right idea, but maybe in the wrong order. First group by
EMPID, WEEKENDING, FACILITYto sum the number of hours per employee, then count theEMPIDs for each case. So, something like: