Ver Mensaje Individual
  #8 (permalink)  
Antiguo 08/05/2014, 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: 18 años, 3 meses
Puntos: 774
Respuesta: Agrupar resultados de una consulta

podria quedar con esto tomando en cuenta los datos que muestras :P

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. Nro INT,
  4. ItemCode    VARCHAR(20),
  5. ItmsGrpCod  INT,
  6. DocDate datetime
  7. )
  8.  
  9. INSERT INTO #temp VALUES (1,'L1-001 EXTRA',115,'2014-03-12 00:00:00.000')
  10. INSERT INTO #temp VALUES (2,'L1-001 EXTRA',115,'2014-03-11 00:00:00.000')
  11. INSERT INTO #temp VALUES (3,'L1-001 EXTRA',115,'2014-03-07 00:00:00.000')
  12. INSERT INTO #temp VALUES (4,'L1-001 EXTRA',115,'2014-03-07 00:00:00.000')
  13. INSERT INTO #temp VALUES (5,'L1-001 EXTRA',115,'2014-03-07 00:00:00.000')
  14. INSERT INTO #temp VALUES (6,'L1-001 EXTRA',115,'2014-03-06 00:00:00.000')
  15. INSERT INTO #temp VALUES (7,'L1-001 EXTRA',115,'2014-02-21 00:00:00.000')
  16. INSERT INTO #temp VALUES (8,'L1-001 EXTRA',115,'2014-02-19 00:00:00.000')
  17. INSERT INTO #temp VALUES (9,'L1-001 EXTRA',115,'2014-02-11 00:00:00.000')
  18. INSERT INTO #temp VALUES (10,'L1-001 EXTRA',115,'2014-02-05 00:00:00.000')
  19. INSERT INTO #temp VALUES (11,'L1-001 EXTRA',115,'2014-02-03 00:00:00.000')
  20. INSERT INTO #temp VALUES (12,'L1-001 EXTRA',115,'2014-02-01 00:00:00.000')
  21. INSERT INTO #temp VALUES (13,'L1-001 EXTRA',115,'2014-01-28 00:00:00.000')
  22. INSERT INTO #temp VALUES (14,'L1-001 EXTRA',115,'2014-01-23 00:00:00.000')
  23. INSERT INTO #temp VALUES (15,'L1-001 EXTRA',115,'2014-01-21 00:00:00.000')
  24. INSERT INTO #temp VALUES (16,'L1-001 EXTRA',115,'2014-01-18 00:00:00.000')
  25. INSERT INTO #temp VALUES (17,'L1-001 EXTRA',115,'2014-01-02 00:00:00.000')
  26. INSERT INTO #temp VALUES (18,'L1-001 EXTRA',115,'2013-12-30 00:00:00.000')
  27. INSERT INTO #temp VALUES (19,'L1-001 EXTRA',115,'2013-12-28 00:00:00.000')
  28. INSERT INTO #temp VALUES (20,'L1-001 EXTRA',115,'2013-12-18 00:00:00.000')
  29. INSERT INTO #temp VALUES (1,'L1-001 NOVEX',115,'2014-03-15 00:00:00.000')
  30. INSERT INTO #temp VALUES (2,'L1-001 NOVEX',115,'2014-03-13 00:00:00.000')
  31. INSERT INTO #temp VALUES (3,'L1-001 NOVEX',115,'2014-03-07 00:00:00.000')
  32. INSERT INTO #temp VALUES (4,'L1-001 NOVEX',115,'2014-03-07 00:00:00.000')
  33. INSERT INTO #temp VALUES (5,'L1-001 NOVEX',115,'2014-03-06 00:00:00.000')
  34. INSERT INTO #temp VALUES (6,'L1-001 NOVEX',115,'2014-03-04 00:00:00.000')
  35. INSERT INTO #temp VALUES (7,'L1-001 NOVEX',115,'2014-02-27 00:00:00.000')
  36. INSERT INTO #temp VALUES (8,'L1-001 NOVEX',115,'2014-02-25 00:00:00.000')
  37. INSERT INTO #temp VALUES (9,'L1-001 NOVEX',115,'2014-02-22 00:00:00.000')
  38. INSERT INTO #temp VALUES (10,'L1-001 NOVEX',115,'2014-02-22 00:00:00.000')
  39. INSERT INTO #temp VALUES (11,'L1-001 NOVEX',115,'2014-02-19 00:00:00.000')
  40.  
  41. SELECT * FROM #temp WHERE Nro BETWEEN 1 AND 5

creo que la falla fue el where final en lugar de WHERE nro <=5

pruebalo con:

Código SQL:
Ver original
  1. SELECT *
  2. FROM
  3. (
  4. SELECT
  5. ROW_NUMBER() OVER(PARTITION BY V.ItemCode ORDER BY V.DocDate DESC) AS [Nro], V.ItemCode, B.ItmsGrpCod, V.DocDate
  6. FROM INV1 V
  7. INNER JOIN OITM M ON V.ItemCode=M.ItemCode
  8. INNER JOIN OITB B ON M.ItmsGrpCod=B.ItmsGrpCod) t1
  9. WHERE nro BETWEEN 1 AND 5

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