计算经验而不重叠

问题描述 投票:-1回答:1

我正在尝试提出正确的查询来计算就业经历时间,但是我做得不好,因此,我感谢您的帮助。这是我的数据:

案例1:

EmployeeID    PoisitionID      StartDate    EndDate
1               15             5/22/2017     5/22/2018
1               17             7/14/2018     8/10/2019

案例2:

EmployeeID     PositonID       StartDate    EndDate
1                15            5/22/2017     8/10/2019
1                17            3/8/2019      8/10/2019

案例3:

EmployeeID     PositonID       StartDate    EndDate
1                15            5/22/2017     NULL
1                17            3/8/2019      NULL
  • 在第一种情况下,我的预期结果以月为单位:两个职位的月度为27个月。

  • 在第二种情况下,我的期望结果以月为单位:positonid 15为27个月,positionid 17为0个月,因为positionid 17属于第一个职位的日期范围,因此该员工将不会获得任何年的经验。

  • 在第三种情况下,我的期望结果以月为单位:使用今天的日期作为positonid 15的结束日期,以30个月为结束日期,以positionid 17的结束日期为0个月,因为positionid 17属于第一个职位的日期范围,因此,员工将不会获得任何多年的经验。

谢谢!

sql sql-server
1个回答
0
投票

您没有任何差距,所以我认为这可以满足您的要求:

select employeeid,
       datediff(month, min(startdate), coalesce(max(enddate), getdate())) as months
from t
group by employeeid;
© www.soinside.com 2019 - 2024. All rights reserved.