Hi,
I have the following scenario in relation to the holding of financial stocks:
On 01-Sept-2011 I bought two seperate lots in a stock -- 50 units and 100 units, as depicted below, for day T:
Day,Lot_Nbr,Units_Hld,Acq_date
==============================
T,1,50,01-Sept-2011
T,2,100,01-Sept-2011
On T+1 I decided to sell the first lot of 50 and 25 of the second lot resulting in:
Day,Lot_Nbr,Units_Hld,Acq_date
==============================
T+1,1,75,01-Sept-2011
Note: as we sold all of Lot 1, the 50 units, Lot 2 now becomes Lot 1.
Now, I am trying to write a SQL query to pull back these changes in LOTS. I am trying to use the LEAD Analytic function but I cant seem to partition it in such a way as to reflect what is happening.
I currently have:
Units_Hld - LEAD (
Units_Hld,
1,
0
)
OVER (
PARTITION BY lot_Nbr
ORDER BY d_date
)
Day
But this is subtracting the 75 from the 50 as it thinks they are the same lot_nbr. If I change it to use acq_date they its taking the 50 from the 100 which is also incorrect.
Any ideas what I can do to recreate the actual scenario in SQL?
Thanks.
I have the following scenario in relation to the holding of financial stocks:
On 01-Sept-2011 I bought two seperate lots in a stock -- 50 units and 100 units, as depicted below, for day T:
Day,Lot_Nbr,Units_Hld,Acq_date
==============================
T,1,50,01-Sept-2011
T,2,100,01-Sept-2011
On T+1 I decided to sell the first lot of 50 and 25 of the second lot resulting in:
Day,Lot_Nbr,Units_Hld,Acq_date
==============================
T+1,1,75,01-Sept-2011
Note: as we sold all of Lot 1, the 50 units, Lot 2 now becomes Lot 1.
Now, I am trying to write a SQL query to pull back these changes in LOTS. I am trying to use the LEAD Analytic function but I cant seem to partition it in such a way as to reflect what is happening.
I currently have:
Units_Hld - LEAD (
Units_Hld,
1,
0
)
OVER (
PARTITION BY lot_Nbr
ORDER BY d_date
)
Day
But this is subtracting the 75 from the 50 as it thinks they are the same lot_nbr. If I change it to use acq_date they its taking the 50 from the 100 which is also incorrect.
Any ideas what I can do to recreate the actual scenario in SQL?
Thanks.