Code :
DECLARE @tmpTable TABLE (IDENTIF char(4), QUESTION char(4), HEURE char(6), REPONSE char(4), TYPE char(1)) INSERT @tmpTable (IDENTIF, QUESTION, HEURE, REPONSE, TYPE) VALUES ('0001','0001','111245','00x1','A'), ('0001','0001','081130','00x2','A'), ('0001','0002','031220','00x3','A'), ('0001','0003','134110','00x4','C'), ('0002','0001','111245','00x5','A'), ('0002','0001','081130','00x6','A'), ('0002','0002','031220','00x7','A'), ('0002','0003','134110','00x8','C') SELECT IDENTIF , MAX(CASE WHEN QUESTION = '0001' THEN REPONSE ELSE '' END) REPONSE1 , MAX(CASE WHEN QUESTION = '0002' THEN REPONSE ELSE '' END) REPONSE2 FROM (SELECT IDENTIF, QUESTION, HEURE, REPONSE, TYPE, ROW_NUMBER() OVER (PARTITION BY IDENTIF, QUESTION ORDER BY IDENTIF, QUESTION, HEURE DESC) RN FROM @tmpTable) a WHERE RN = 1 GROUP BY IDENTIF
|