Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. WHEN RunningTotal >= ReceivedQuantity THEN increment to next Purchase Price.
  2.  
  3. CREATE TABLE dbo.TEMP_PurchaseOrder
  4. (
  5. POID NCHAR(7) NOT NULL
  6. ,ItemID NCHAR(1) NOT NULL
  7. ,RecievedDate INT NOT NULL
  8. ,Received_quantity DECIMAL(17,6) NOT NULL
  9. ,Purchase_price DECIMAL(17,6) NOT NULL
  10. );
  11.  
  12. INSERT INTO TEMP_PurchaseOrder
  13. VALUES
  14. (1000100,1,'20120401',10.000000,5.100000)
  15. ,(1000104,1,'20120410',10.000000,5.100000)
  16. ,(1000105,1,'20120420',10.000000,5.050000)
  17. ,(1000101,2,'20120401',10.000000,15.000000)
  18. ,(1000102,2,'20120401',10.000000,15.000000)
  19. ,(1000103,2,'20120409',10.000000,14.500000);
  20.  
  21. CREATE TABLE dbo.TEMP_Transactions
  22. (
  23. OrderID NCHAR(7) PRIMARY KEY
  24. ,ItemID NCHAR(15) NOT NULL
  25. ,TransDate INT NOT NULL
  26. ,POID NCHAR(7) NOT NULL
  27. ,TransQuant DECIMAL(15,6) NOT NULL
  28. );
  29.  
  30. INSERT INTO TEMP_Transactions
  31. VALUES
  32. (1000100,1,'20120401',100,10.000000)
  33. ,(1000002,1,'20120401',0,-5.000000)
  34. ,(1000003,1,'20120401',0,-2.000000)
  35. ,(1000004,1,'20120407',0,-3.000000)
  36. ,(1000104,1,'20120410',104,10.000000)
  37. ,(1000006,1,'20120412',0,-1.000000)
  38. ,(1000007,1,'20120415',0,-8.000000)
  39. ,(1000105,1,'20120420',105,10.000000)
  40. ,(1000101,2,'20120401',101,10.000000)
  41. ,(1000102,2,'20120401',102,10.000000)
  42. ,(1000011,2,'20120402',0,-5.000000)
  43. ,(1000012,2,'20120405',0,-2.000000)
  44. ,(1000013,2,'20120405',0,-4.000000)
  45. ,(1000014,2,'20120406',0,-5.000000)
  46. ,(1000015,2,'20120408',0,-1.000000)
  47. ,(1000103,2,'20120409',103,10.000000);
  48.  
  49. Begin
  50. ;With trans as
  51. (
  52. Select distinct
  53. TransDate
  54. ,ItemID
  55. --,POID
  56. ,RunningSold = abs(SUM(sum(case when POID = '0' then cast(TransQuant as Int) else 0 end)) OVER (PARTITION BY ItemID Order by TransDate ROWS UNBOUNDED PRECEDING))
  57. ,RunningRecieved = SUM(sum(case when POID > '0' then cast(TransQuant as Int) else 0 end)) OVER (PARTITION BY ItemID Order by TransDate ROWS UNBOUNDED PRECEDING)
  58. From dbo.TEMP_Transactions
  59. Group by TransDate,ItemID--,POID
  60. )
  61. SELECT distinct t.TransDate, t.ItemID, (t.RunningRecieved - t.RunningSold) as RunningTotal
  62. FROM trans t
  63. Where t.ItemID = '1'
  64. ORDER BY t.TransDate, t.ItemID;
  65. END
  66.  
  67. DROP TABLE dbo.TEMP_PurchaseOrder;
  68. DROP TABLE dbo.TEMP_Transactions;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement