Code :
Declare @tmpTable TABLE (NUMSS int, DEB datetime, FIN datetime, Libemploi varchar(50)) INSERT @tmpTable (NUMSS, DEB, FIN, Libemploi) VALUES (17807280, '2010/01/01', NULL, 'ACHETEUR'), (17807280, '2009/07/01', '2009/12/31', 'ACHETEUR'), (17807280, '2009/01/01', '2009/06/30', 'RESP. LIBRE SERVICE'), (17807280, '2005/11/01', '2008/12/31', 'ASSISTANT(E) TRANSPORT'), (17807280, '2004/01/01', '2005/10/31', 'ASSISTANT(E) TRANSPORT'), (17807280, '2003/04/01', '2003/12/31', 'ASSISTANT(E) TRANSPORT'), (17807280, '2001/10/01', '2003/03/31', 'ACHETEUR') SELECT NUMSS, MIN(DEB) DEB, CASE WHEN MAX(isnull(FIN, '9999/12/31')) = '9999/12/31' THEN NULL ELSE MAX(isnull(FIN, '9999/12/31')) END FIN, Libemploi FROM ( SELECT ROW_NUMBER() over (partition BY libemploi ORDER BY NUMSS, DEB DESC, libemploi) - ROW_NUMBER() over (ORDER BY NUMSS, DEB DESC, libemploi) a, NUMSS, DEB, FIN, Libemploi FROM @tmpTable ) a GROUP BY a.a, NUMSS, Libemploi ORDER BY a.a DESC
|