我有一些问题,我希望最早的工作开始,最后的工作以每个职位结束。
表格中的数据:
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