Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. 2 TABLES (portfolio and Transactions)
  2. PORTFOLIO
  3. Exportdate Accno Marketvalue
  4. 01.01.2017 1 100
  5. 01.01.2017 2 50
  6. 01.01.2017 3 200
  7. 02.01.2017 1 140
  8. 02.01.2017 2 70
  9. 02.01.2017 3 210
  10. 03.01.2017 1 120
  11. 03.01.2017 2 70
  12. 03.01.2017 3 256
  13.  
  14.  
  15. TRANSACTIONS
  16. Exportdate Accno Amt
  17. 01.01.2017 1 100
  18. 01.01.2017 2 50
  19. 01.01.2017 3 200
  20. 02.01.2017 1 11
  21. 02.01.2017 2 2
  22. 02.01.2017 3 4
  23. 03.01.2017 1 5
  24. 03.01.2017 2 7
  25. 03.01.2017 3 1
  26.  
  27.  
  28. My desired result:
  29. MYQUERY (costprice)
  30. Exportdate accno marketvalue sumAmt Return
  31. 01.01.2017 1 100 100 0
  32. 01.01.2017 2 50 50 0
  33. 01.01.2017 3 200 200 0
  34. 02.01.2017 1 140 111 29
  35. 02.01.2017 2 70 52 18
  36. 02.01.2017 3 210 204 6
  37. 03.01.2017 1 120 116 4
  38. 03.01.2017 2 70 59 11
  39. 03.01.2017 3 256 205 51
  40.  
  41. SELECT
  42. x.ExportDate
  43. , x.AccNo
  44. , x.MarketValue
  45. , lag(x.marketvalue) OVER (ORDER BY x.portofolioID, x.accno) as last
  46.  
  47. ,(SELECT
  48. SUM(y.amt) OVER(ORDER BY y.exportdate, y.accno)
  49. from myDB.dbo.view_trans y
  50. where y.exportdate <= x.exportdate and y.AccNo = x.accno) as costprice
  51.  
  52. FROM MyDb.dbo.Portofolio as X
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement