Tema: Query join
Ver Mensaje Individual
  #2 (permalink)  
Antiguo 04/08/2014, 14:27
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 9 meses
Puntos: 774
Respuesta: Query join

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. id INT,
  4. nombre VARCHAR(20),
  5. fecha datetime
  6. )
  7.  
  8.  
  9.  
  10. INSERT INTO #temp VALUES (1,'roger','01/05/2014')
  11. INSERT INTO #temp VALUES (1,'jorge','01/07/2014')
  12. INSERT INTO #temp VALUES (1,'ana','03/15/2014')
  13. INSERT INTO #temp VALUES (2,'perez','01/27/2014')
  14. INSERT INTO #temp VALUES (3,'lucia','05/13/2014')
  15. INSERT INTO #temp VALUES (3,'alberto','01/10/2014')
  16. INSERT INTO #temp VALUES (4,'Ilda','07/01/2014')
  17.  
  18. SELECT t1.* FROM #temp AS t1
  19. INNER JOIN (SELECT MAX(fecha) fecha,id FROM #temp GROUP BY id ) AS t2 ON (t1.fecha=t2.fecha AND t1.id=t2.id)
  20. ORDER BY id ASC

Resultado:

id nombre fecha
----------- -------------------- -----------------------
1 ana 2014-03-15 00:00:00.000
2 perez 2014-01-27 00:00:00.000
3 lucia 2014-05-13 00:00:00.000
4 Ilda 2014-07-01 00:00:00.000
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me