sql - 计算经验而不重叠
问题描述
我正在尝试提出正确的查询来计算就业经验时间,但是我做错了。这是我拥有的数据:
情况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
在第一种情况下,我在几个月内的预期结果是:两个职位都需要 27 个月。
在第二种情况下,我的预期结果是:positonid 15 为 27 个月,positionid 17 为 0 个月,因为 positionid 17 属于第一个职位的日期范围,因此,该员工不会获得任何年的经验.
在第三种情况下,我的预期结果是:30 个月使用今天的日期作为 positonid 15 的结束日期,使用 0 个月作为 positionid 17 的结束日期,因为 positionid 17 落在第一个职位的日期范围内,因此,员工不会授予任何多年的经验。
解决方案
你没有任何差距,所以我认为这是你想要的:
select employeeid,
datediff(month, min(startdate), coalesce(max(enddate), getdate())) as months
from t
group by employeeid;
推荐阅读
- angular - Angular 语言服务不检查 html 错误
- azure-sql-database - 将 Power BI 数据集实时转换为 Azure SQL 数据库的可能性?
- angular - 为什么Angular Oninit仅在离开页面时才调用服务
- mysql - 如何根据特定类别限制 MySQL 表中的记录数
- kubernetes - 如何将 GKE 与在不同项目的 VM 上运行的外部数据库连接?
- jenkins - 在詹金斯中添加服务挂钩时出错。需要权限才能执行此操作“创建 StrongBox 抽屉”
- nginx - 如何根据 nginx 中的子域将流量路由到不同的端口?
- object - 需要传递哪个值来设置标准帐户对象的父子关系
- java - Spring WebClient 缓冲列表缺少最后一个信号?
- react-native - React Native Reanimated ScrollView 无限水平