Possible Duplicate:
Trying to consolidate employer records who are continuously work for same department
I am trying to consolidate employees records who have been continuously (anything < 45 days) enrolled with the specific department
Note: If the date diff (between emp_eff_to_date
and next row emp_eff_from_date
) is less than 45 days then it is considered as continuous
INPUT:
EMP_ID + DEPT_ID + EMP_EFF_FROM_DATE + EMP_EFF_TO_DATE
-----------------------------------------------------------------------
10 10001 8/1/2008 10/31/2009
10 10001 11/1/2009 2/25/2010
10 10001 2/26/2010 5/1/2011
10 10001 8/1/2011 10/30/2011
10 10001 12/1/2011 10/31/2012
10 10003 7/1/2007 10/31/2007
10 10004 9/27/2004 6/8/2006
10 10004 6/30/2006 6/29/2007
10 10007 6/25/2006 6/20/2007
10 10007 8/25/2007 5/25/2008
Output desired:
EMP_ID DEPT_ID EMP_EFF_FROM_DATE EMP_EFF_TO_DATE
-------------------------------------------------------------------------
10 10001 2008-08-01 2011-05-01
10 10001 2011-08-01 2012-10-31
10 10003 2007-07-01 2007-10-31
10 10004 2004-09-27 2007-06-29
10 10007 2006-06-25 2007-06-20
10 10007 2007-08-25 2007-06-29