sql查询,以捕获最小日期,最大日期为基础的行排序高达空

41zrol4v  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(313)

议程是跟踪资源的招聘和再招聘场景

  1. empID emp_Status CreateDate end_date
  2. 1 active **2019-02-01**Null
  3. 1 active 2019-02-02 2019-01-04 (Skipped because last day is greater than joining date)
  4. 1 active 2019-02-03 Null
  5. 1 Terminated 2019-02-04 **2019-02-02**
  6. 1 Terminated 2019-02-05 2019-02-02
  7. 1 active 2019-02-06 Null

输出应能够跟踪加入日期和最后一个工作日和各自的计数
输出:

  1. empID join_date last_date Joining_count
  2. 1 2019-02-01 2019-02-02 1
  3. 1 2019-02-06 Null 2

我需要在redshift或oraclesql查询中实现这一点。请帮我渡过难关。

7fhtutme

7fhtutme1#

如果我理解正确,对于每个“新”活动,您希望下一个“终止”。一种方法是创建反向计数为终止的组,然后进行聚合:

  1. select empid,
  2. min(case when emp_status = 'active' then createdate end) as active_date,
  3. min(case when emp_status = 'Terminated' then createdate end) as terminate_date,
  4. row_number() over (partition by empid order by min(createdate)) as joining_count
  5. from (select t.*,
  6. sum(case when emp_status = 'Terminated' then 1 else 0 end) over (partition by empid order by createdate desc) as grp
  7. from t
  8. ) t
  9. group by empid, grp
  10. having sum(case when emp_status = 'active' then 1 else 0 end) > 0;

这是一把小提琴。

相关问题