Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. order_product_table
  2. -------------------------
  3.  
  4. id | date | Product_id | value
  5. -------------------------------------------
  6. 1 | 2017-07-01 | 2 | 53
  7. 2 | 2017-08-05 | 2 | 67
  8. 3 | 2017-10-02 | 2 | 83
  9. 4 | 2018-01-20 | 5 | 32
  10. 5 | 2018-05-01 | 5 | 53
  11. 6 | 2008-08-05 | 6 | 67
  12.  
  13.  
  14. Transfer_product_table
  15. ----------------------------
  16.  
  17. id | date | Product_id | value
  18. --------------------------------------------
  19. 1 | 2017-08-01 | 2 | 10
  20. 2 | 2017-10-06 | 2 | 20
  21. 3 | 2017-12-12 | 2 | 31
  22. 4 | 2018-06-25 | 5 | 5
  23.  
  24. Result(Transfer_product_table)
  25. --------------------------------
  26. id | date | Product_id | value
  27. --------------------------------------------
  28. 1 | 2017-08-01 | 2 | 53
  29. 2 | 2017-10-06 | 2 | 83
  30. 3 | 2017-12-12 | 2 | 83
  31. 4 | 2018-06-25 | 5 | 53
  32.  
  33. UPDATE Transfer_product_table imp
  34. SET value = sub.value
  35. FROM (SELECT product_id,value
  36. ,ROW_NUMBER() OVER (PARTITION BY product_id ORDER BY orderdate DESC)AS Rno
  37. FROM order_product_table
  38. where orderdate between '2017-07-01' and '2019-10-31') sub
  39.  
  40. WHERE imp.product_id = sub.product_id
  41. and sub.Rno=1
  42. and imp.date between '2017-07-01' and '2019-10-31'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement