Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WHEN RunningTotal >= ReceivedQuantity THEN increment to next Purchase Price.
- CREATE TABLE dbo.TEMP_PurchaseOrder
- (
- POID NCHAR(7) NOT NULL
- ,ItemID NCHAR(1) NOT NULL
- ,RecievedDate INT NOT NULL
- ,Received_quantity DECIMAL(17,6) NOT NULL
- ,Purchase_price DECIMAL(17,6) NOT NULL
- );
- INSERT INTO TEMP_PurchaseOrder
- VALUES
- (1000100,1,'20120401',10.000000,5.100000)
- ,(1000104,1,'20120410',10.000000,5.100000)
- ,(1000105,1,'20120420',10.000000,5.050000)
- ,(1000101,2,'20120401',10.000000,15.000000)
- ,(1000102,2,'20120401',10.000000,15.000000)
- ,(1000103,2,'20120409',10.000000,14.500000);
- CREATE TABLE dbo.TEMP_Transactions
- (
- OrderID NCHAR(7) PRIMARY KEY
- ,ItemID NCHAR(15) NOT NULL
- ,TransDate INT NOT NULL
- ,POID NCHAR(7) NOT NULL
- ,TransQuant DECIMAL(15,6) NOT NULL
- );
- INSERT INTO TEMP_Transactions
- VALUES
- (1000100,1,'20120401',100,10.000000)
- ,(1000002,1,'20120401',0,-5.000000)
- ,(1000003,1,'20120401',0,-2.000000)
- ,(1000004,1,'20120407',0,-3.000000)
- ,(1000104,1,'20120410',104,10.000000)
- ,(1000006,1,'20120412',0,-1.000000)
- ,(1000007,1,'20120415',0,-8.000000)
- ,(1000105,1,'20120420',105,10.000000)
- ,(1000101,2,'20120401',101,10.000000)
- ,(1000102,2,'20120401',102,10.000000)
- ,(1000011,2,'20120402',0,-5.000000)
- ,(1000012,2,'20120405',0,-2.000000)
- ,(1000013,2,'20120405',0,-4.000000)
- ,(1000014,2,'20120406',0,-5.000000)
- ,(1000015,2,'20120408',0,-1.000000)
- ,(1000103,2,'20120409',103,10.000000);
- Begin
- ;With trans as
- (
- Select distinct
- TransDate
- ,ItemID
- --,POID
- ,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))
- ,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)
- From dbo.TEMP_Transactions
- Group by TransDate,ItemID--,POID
- )
- SELECT distinct t.TransDate, t.ItemID, (t.RunningRecieved - t.RunningSold) as RunningTotal
- FROM trans t
- Where t.ItemID = '1'
- ORDER BY t.TransDate, t.ItemID;
- END
- DROP TABLE dbo.TEMP_PurchaseOrder;
- DROP TABLE dbo.TEMP_Transactions;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement