Ver Mensaje Individual
  #3 (permalink)  
Antiguo 31/05/2011, 08:16
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: Problema de consulta en sql

Un poco rebuscado el query jejejje
Código SQL:
Ver original
  1. CREATE TABLE #temp (
  2. id INT,
  3. fecha datetime
  4. )
  5.  
  6. CREATE TABLE #temp2 (
  7. id INT,
  8. codarticulo INT,
  9. precio INT
  10. )
  11.  
  12.  
  13. INSERT INTO #temp (id,fecha) VALUES (1,getdate()-1)
  14. INSERT INTO #temp (id,fecha) VALUES (2,getdate()-2)
  15. INSERT INTO #temp (id,fecha) VALUES (3,getdate()-3)
  16. INSERT INTO #temp (id,fecha) VALUES (4,getdate()-4)
  17.  
  18. INSERT INTO #temp2 (id,codarticulo,precio) VALUES (1,1,25)
  19. INSERT INTO #temp2 (id,codarticulo,precio) VALUES (2,1,15)
  20. INSERT INTO #temp2 (id,codarticulo,precio) VALUES (3,1,35)
  21. INSERT INTO #temp2 (id,codarticulo,precio) VALUES (4,1,23)
  22. INSERT INTO #temp2 (id,codarticulo,precio) VALUES (1,2,51)
  23. INSERT INTO #temp2 (id,codarticulo,precio) VALUES (2,2,28)
  24. INSERT INTO #temp2 (id,codarticulo,precio) VALUES (3,2,13)
  25.  
  26. SELECT codarticulo,fecha,precio FROM #temp t
  27. INNER JOIN #temp2 t1 ON (t.id=t1.id)
  28. WHERE CONVERT(VARCHAR(20),fecha) + '|' + CONVERT(VARCHAR(20),codarticulo) IN (
  29. SELECT CONVERT(VARCHAR(200),MAX(fecha)) + '|' +  CONVERT(VARCHAR(20),codarticulo) FROM #temp t
  30. INNER JOIN #temp2 t1 ON (t.id=t1.id)
  31. GROUP BY codarticulo
  32. )

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