sql - How to get the first and last dates/records from the employee history table -
lets emp hired on 10 apr 15 , transferred on 30 apr 15 , again promoted on 10 june 15. history need first , last (recent record) emp_data. action_date dates mentioned above. need 10 apr , 10 june output query. action refereed hire, transfer, etc.
this attempting-
select empid, action, action_dt ps_job action_dt in (select min(action_dt), max(action_dt) ps_job empid='88888'); but instead showing me 3 dates record.
i think have modify query following:
select empid, action, action_dt ps_job action_dt in (select min(action_dt) ps_job empid='88888' union select max(action_dt) ps_job empid='88888') min , max should selected two separate rows, in clause works. 1 way use of union in above query.
alternatively can use row_number:
select empid, action, action_dt ( select empid, action, action_dt, row_number() on (partition empid order action_dt) frn, row_number() on (partition empid order action_dt desc) lrn ps_job ) t frn = 1 or lrn = 1 note version not equivalent first query, there may more 1 records having same max or min date. have replace row_number rank if want all max, min records selected.
Comments
Post a Comment