我有一名员工已加入两次。请查看下表。
EmpID EmpName DateOFJoin DateOfLeaving Status
1 XYZ 2015-10-01 2017-09-26 De-Active
2 ABC 2018-01-01 Active
3 XYZ 2018-10-15 Active
我想要像这样的输出 例如, 我有 FromDate 和 ToDate,例如“2019-12-01”和“2019-12-31”
EmpID EmpName DateOFJoin DateOfLeaving Status
2 ABC 2018-01-01 Active
3 XYZ 2018-10-15 Active
如果我有像“2017-08-01”和“2017-09-30”这样的 FromDate 和 ToDate
EmpID EmpName DateOFJoin DateOfLeaving Status
1 XYZ 2015-10-01 2017-09-26 De-Active
如果我有 FromDate 和 ToDate,例如“2018-01-01”和“2018-03-31”
EmpID EmpName DateOFJoin DateOfLeaving Status
2 ABC 2018-01-01 Active
请帮忙准备SQL。
最佳答案
使用coalesce()
函数
<强> DEMO
select EmpID,EmpName,DateOFJoin,DateOfLeaving,Status
from tablename
where DateOFJoin<=param1 and coalesce(DateOfLeaving,param2)>=param2
关于SQL 查询员工入职日期和离职日期之间的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54900058/