每个员工每周工作的SQL计数班次

时间:2016-10-20 16:52:03

标签: sql

我想构建一个查询,显示我的员工每天工作的次数。例如,我想表明,在一段时间内,吉姆工作了12个星期六,3个星期一和星期四。我打算通过从时钟条目中提取工作日datepart来做到这一点。我只是不知道如何构建它,以便有七个条件计数列。

提前致谢

1 个答案:

答案 0 :(得分:0)

做出很多假设:

      
  1. “TimeCard”是您的表格
  2.   
  3. EmployeeID是您的员工标识符
  4.   
  5. 每位员工每天有1次时间记录
  6.   
  7. 员工工作的日期是姓名[timestamp]
  8. 
    select 
        EmployeeID,
        count(case when datepart(weekday, [timestamp]) = 1 then 1 end) as [Sunday],
        count(case when datepart(weekday, [timestamp]) = 2 then 1 end) as [Monday],
        count(case when datepart(weekday, [timestamp]) = 3 then 1 end) as [Tuesday],
        count(case when datepart(weekday, [timestamp]) = 4 then 1 end) as [Wednesday],
        count(case when datepart(weekday, [timestamp]) = 5 then 1 end) as [Thursday],
        count(case when datepart(weekday, [timestamp]) = 6 then 1 end) as [Friday],
        count(case when datepart(weekday, [timestamp]) = 7 then 1 end) as [Saturday]
    from [TimeCard]
    group by EmployeeID