在SQL中,如何获得员工工作的每个连续职位的最小起始日期和最长结束日期?

时间:2017-12-05 22:35:43

标签: sql

我有一些问题,我希望最早的工作开始,最后的工作以每个职位结束。

表格中的数据:

   EMPLOYEE_ID      PCN         POSITION    WorkBEG                 WorkEND  
    --16156         056-240-35  056FOOD706  2014-09-18 00:00:00.000 2015-06-04 00:00:00.000  
    --16156         056-240-35  056FOOD706  2015-08-20 00:00:00.000 2015-08-23 00:00:00.000  
    --16156         057-240-35  057FOOD706  2015-08-24 00:00:00.000 2016-06-02 00:00:00.000  
    --16156         057-240-35  057FOOD706  2016-08-18 00:00:00.000 2017-06-02 00:00:00.000  
    --16156         TEMP        715SUMREMB  2017-06-05 00:00:00.000 2017-08-13 00:00:00.000  
    --16156         057-240-35  057FOOD706  2017-08-14 00:00:00.000 2018-06-01 00:00:00.000  

- 我的查询
- 3行

select m.ID as EMPLOYEE_ID,  
  p.PCN,  
  p.POSITION,  
  min(p.CALC_BEG) as WorkBEG,  
  max(p.CALC_END) as WorkEND  
from hr_empmstr m  
  inner join hr_emppay p on p.ID = m.ID  
where (m.LASTDAYWRK is null  
  or m.LASTDAYWRK >= dateadd(y,-5,getdate()))  
and p.REC_TYPE = 'PM'  
and p.CALC_END <> p.CALC_BEG  
and m.ID = '16156'  
group by m.ID, p.PCN, p.POSITION  
order by m.ID,  WorkBEG;  

结果:

--EMPLOYEE_ID   PCN         POSITION    WorkBEG                 WorkEND  
--16156         056-240-35  056FOOD706  2014-09-18 00:00:00.000 2015-08-23 00:00:00.000  
--16156         057-240-35  057FOOD706  2015-08-24 00:00:00.000 2018-06-01 00:00:00.000  
--16156         TEMP        715SUMREMB  2017-06-05 00:00:00.000 2017-08-13 00:00:00.000  

试图获得:

--EMPLOYEE_ID   PCN         POSITION    POS_LONG                        WorkBEG                 WorkEND  
--16156         056-240-35  056FOOD706  FOOD SERV/CAFE HELP             2014-09-18 00:00:00.000 2015-08-23 00:00:00.000  
--16156         057-240-35  057FOOD706  FOOD SERV/CAFE HELP             2015-08-24 00:00:00.000 2017-06-02 00:00:00.000  
--16156         TEMP        715SUMREMB  SUMMER EMPLOYEE                 2017-06-05 00:00:00.000 2017-08-13 00:00:00.000  
--16156         057-240-35  057FOOD706  FOOD SERV/CAFE HELP             2017-08-14 00:00:00.000 2018-06-01 00:00:00.000  

0 个答案:

没有答案