I have a report that determines employees that have entered less than 40 hours on their timesheets for a specific week:
With Under40s as (
Select Distinct Entry.UUID, Entry.FirstName, Entry.LastName, Team.PersonUnit, Team.TeamName
From Entry as Entry
Inner Join TeamOrganization as Team on Team.PersonUUID = Entry.UUID and @EnteredDate between Team.PeriodBeginDate and Team.PeriodEndDate
Where
(
Select sum(Entry2.Hours) From Entry as Entry2
Where Entry2.UUID = Entry.UUID and Entry2.Date Between @StartDate and @EndDate
) < 40
Or not exists
(
Select 1 From Entry as Entry2
Where Entry2.UUID = Entry.UUID and Entry2.Date Between @StartDate and @EndDate
)
)
Select distinct Under40s.UUID, Under40s.FirstName, Under40s.LastName, Under40s.PersonUnit, Under40s.TeamName, Case
When Sum(Entry.Hours) is null then 0
Else Sum(Entry.Hours)
End as TotalHours
From Under40s
Left Join Entry as Entry on Entry.UUID = Under40s.UUID and Entry.Date Between @StartDate and @EndDate
Where Under40s.PersonUnit in (@PersonUnit) and Under40s.TeamName in (@Teams)
Group By Under40s.UUID, Under40s.FirstName, Under40s.LastName, Under40s.PersonUnit, Under40s.TeamName
Order By Under40s.LastName, Under40s.FirstName
The client now wants to be able to enter a date range of more than a week. I'm thinking they want to see for each week within that date range, who's reporting less than 40 hours. I am not sure how, or if, I can modify the SQL to deliver such a report. Can anyone give me a hint?
Thanks!