如何让每季度完成5年工作的员工

时间:2019-03-18 11:05:15

标签: sql stored-procedures

我试图让那些完成了5,10,15,20等年的员工。 我想每季度检索一次数据 所以我在where子句中添加了switch语句

declare @CurrentDate date

set @CurrentDate=GETDATE() ;

select EmployeeID,FirstName+''+isnull(MiddleName,'')+''+LastName as EmployeeName,JoiningDate,DATEDIFF(year, isnull(JoiningDate,''),getdate()) Experience

 from HRIS.dbo.EmpMaster
 where 
 ActiveFlag=1 and
CASE
    WHEN  DATEPART(quarter, getdate()) =1 and month(JoiningDate) BETWEEN 1 and 3 then 1
    WHEN  DATEPART(quarter, getdate()) =2 and month(JoiningDate) BETWEEN 4 and 6 then 1
    WHEN  DATEPART(quarter, getdate()) =3 and month(JoiningDate) BETWEEN 7 and 9 then 1
    WHEN  DATEPART(quarter, getdate()) =4 and month(JoiningDate) BETWEEN 10 and 12 then 1
    else 0
    END
 and  DATEDIFF(year, isnull(JoiningDate,''),@CurrentDate)%5=0 and Year(JoiningDate)!=2019

我遇到如下错误

Error: An expression of non-boolean type specified in a context where a condition is expected, near 'and'.

我理解该错误是因为where子句中的情况未返回布尔值。

如何编写开关盒以使其返回布尔值

提前谢谢!

1 个答案:

答案 0 :(得分:0)

嗯。 。 。为什么不简化呢?

WHERE ActiveFlag = 1 AND
      DATEPART(quarter, getdate()) = DATEPART(quarter, JoiningDate)

这似乎简单得多。

将在= 1之后添加END来解决您的问题。但是我不推荐这种方法。