首页 > 解决方案 > 计算经验而不重叠

问题描述

我正在尝试提出正确的查询来计算就业经验时间,但是我做错了。这是我拥有的数据:

情况1:

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

案例二:

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

标签: sqlsql-server

解决方案


你没有任何差距,所以我认为这是你想要的:

select employeeid,
       datediff(month, min(startdate), coalesce(max(enddate), getdate())) as months
from t
group by employeeid;

推荐阅读