Ver Mensaje Individual
  #2 (permalink)  
Antiguo 03/07/2014, 13:57
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, 8 meses
Puntos: 774
Respuesta: Valores más usados en una consulta

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. nombre VARCHAR(20),
  4. color VARCHAR(10),
  5. numero INT
  6. )
  7.  
  8. INSERT INTO #temp VALUES ('Raul','Negro',3)
  9. INSERT INTO #temp VALUES ('Alberto','Verde',4)
  10. INSERT INTO #temp VALUES ('Carlos','Rojo',5)
  11. INSERT INTO #temp VALUES ('Carlos','Amarillo',2)
  12. INSERT INTO #temp VALUES ('Alberto','Gris',3)
  13. INSERT INTO #temp VALUES ('Isabel','Azul',4)
  14. INSERT INTO #temp VALUES ('Manuel','Rojo',5)
  15. INSERT INTO #temp VALUES ('Carlos','Negro',1)
  16. INSERT INTO #temp VALUES ('Raul','Azul',6)
  17.  
  18. SELECT t1.* FROM #temp AS t1
  19. LEFT JOIN (SELECT nombre,MAX(numero) numero FROM #temp  GROUP BY nombre) AS t2 ON (t1.nombre=t2.nombre AND t1.numero=t2.numero)
  20. WHERE t2.nombre IS NOT NULL

nombre color numero
-------------------- ---------- -----------
Raul Azul 6
Manuel Rojo 5
Isabel Azul 4
Carlos Rojo 5
Alberto Verde 4


saludos!
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me