使SQL适应多周工作

时间:2013-11-18 15:47:04

标签: sql-server

我有一份报告,确定在特定一周的时间表上输入时间少于40小时的员工:

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

客户现在希望能够输入超过一周的日期范围。我想他们希望在该日期范围内每周看一次,报告的时间不到40小时。我不知道如何或者是否可以修改SQL以提供此类报告。谁能给我一个提示?

谢谢!

1 个答案:

答案 0 :(得分:0)

假设每个工作日是8小时,一个选项是获取开始日期和结束日期之间的日差异,然后将其乘以8并使用产品作为标准。例如:startdate:2013年11月20日和enddate:2013年12月31日是41天,包括周末(所以你也必须过滤周末或非工作日),但就像例子41 * 8 = 328。所以而不是少于40,它将小于328.这是一个链接到一个解决周末和其他指定日期的工作解决方案:Number of days in date range, excluding weekends and other dates, in C#