Guest User

Untitled

a guest
Apr 26th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. SELECT i.*,
  2. LAST_VALUE(i.QTY_END) OVER (PARTITION BY YEAR(i.datephysical),
  3. MONTH(i.datephysical),
  4. i.itemid,
  5. i.inventlocationid
  6. ORDER BY YEAR(i.datephysical) ASC,
  7. MONTH(i.datephysical) ASC,
  8. i.datephysical ASC
  9. ROWS BETWEEN CURRENT ROW
  10. AND UNBOUNDED FOLLOWING) AS last_val
  11. INTO #CTE
  12. FROM [tmp].[dbo].[Test] i WITH (NOLOCK)
  13. JOIN [tmp].[dbo].[DATELIST] d WITH (NOLOCK) ON i.datephysical = d.datelist
  14. WHERE YEAR(d.datelist) = 2016
  15. AND ITEMID = 'G_K100600'
  16. AND i.QTY_MOVE != 0
  17.  
  18. SELECT DISTINCT
  19. ItemID,
  20. B.LastDayMonth
  21. INTO #ITEM
  22. FROM #CTE AS A
  23. CROSS JOIN #DateList AS B
  24.  
  25. SELECT DISTINCT
  26. B.ItemID,
  27. A.INVENTLOCATIONID,
  28. B.LastDayMonth,
  29. A.last_val
  30. INTO #Result
  31. FROM #CTE AS A
  32. FULL JOIN #ITEM AS B ON B.LastDayMonth = A.LastDayMonth
  33. AND B.ITEMID = A.ITEMID
  34. ORDER BY B.LastDayMonth ASC
  35.  
  36. SELECT t.*,
  37. ISNULL(LAST_Val, LAG(Last_Val) OVER (PARTITION BY YEAR(t.LastDayMonth),
  38. MONTH(t.LastDayMonth)--,
  39. --t.LastDayMonth
  40. ORDER BY ITEMID,LastDayMonth ASC)
  41. ) AS wf
  42. FROM (
  43. SELECT a.ITEMID,
  44. a.LastDayMonth,
  45. SUM(a.Last_Val) as [Last_Val]
  46. FROM #Result a
  47. GROUP BY ITEMID,
  48. LastDayMonth ) t
Add Comment
Please, Sign In to add comment