Ver Mensaje Individual
  #4 (permalink)  
Antiguo 10/03/2015, 16:07
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: Consultar ultimo precio ingresado de mi tabla stock

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. id_stock INT,
  4. id_producto INT,
  5. precio DECIMAL(10,6)
  6. )
  7.  
  8. INSERT INTO #temp VALUES (0800,0015,4.50)
  9.  
  10. INSERT INTO #temp VALUES (0820
  11. ,0015
  12. ,5.00)
  13.  
  14. INSERT INTO #temp VALUES (0824
  15. ,0015
  16. ,5.00)
  17.  
  18. INSERT INTO #temp VALUES (0827
  19. ,0018
  20. ,3.00)
  21.  
  22. INSERT INTO #temp VALUES (0901
  23. ,0015
  24. ,4.00)
  25.  
  26. INSERT INTO #temp VALUES (0904
  27. ,0018
  28. ,3.5)
  29.  
  30.  
  31. SELECT t1.* FROM #temp AS t1
  32. LEFT JOIN (SELECT MAX(id_stock) stock, id_producto FROM #temp GROUP BY id_producto) AS t2 ON (t1.id_producto=t2.id_producto AND t1.id_stock=t2.stock)
  33. WHERE t2.stock IS NOT NULL

Resultado:

id_stock id_producto precio stock id_producto
904 18 3.500000 904 18
901 15 4.000000 901 15
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me