JapanFreak
Registered User.
- Local time
- Today, 14:03
- Joined
- Aug 25, 2007
- Messages
- 45
Dear All,
please find attached a small sample database which maybe better helps to explain my problem. I guess the solution is quite simple but I just don’t get it…
I have a database with a table called tblTrades in which I collect data on stock market transactions:
Note:
If iQuantity > 0 it is a Buy, if iQuantity < 0 it is a Sell.
I can use the following query to get the list of stocks in my portfolio as per today or, if I add a WHERE clause (e.g. WHERE dtDate<=#01/04/2011#), for a certain point in time in the past:
Note:
If sum(T.iQuantity) > 0 the trade is still open, i.e. the stock is part of the portfolio
If sum(T.iQuantity) = 0 the trade has been closed, i.e. the stock is no longer part of the portfolio
However, I struggle to formulate a query which gives me only the trades for the stocks which are in the portfolio currently (and have been part of the portfolio at a certain point in time in the past, respectively). That is, based on my sample data I would like to have the following result as per today:
…and for a certain date in the past (e.g. 01/04/2011):
Any ideas?
Thanks for your help –
JapanFreak
please find attached a small sample database which maybe better helps to explain my problem. I guess the solution is quite simple but I just don’t get it…
I have a database with a table called tblTrades in which I collect data on stock market transactions:
Code:
[B][I][FONT=Arial]tblTrades[/FONT][/I][/B]
[FONT=Arial]TradeID – dtDate – sTicker – iQuantity – dPrice[/FONT]
[FONT=Arial]1 – 01/03/2011 – A – 10 – 50.00[/FONT]
[FONT=Arial]2 – 01/03/2011 – B – 10 – 25.00[/FONT]
[FONT=Arial]…[/FONT]
Note:
If iQuantity > 0 it is a Buy, if iQuantity < 0 it is a Sell.
I can use the following query to get the list of stocks in my portfolio as per today or, if I add a WHERE clause (e.g. WHERE dtDate<=#01/04/2011#), for a certain point in time in the past:
Code:
[FONT=Arial]SELECT T.sTicker, sum(T.iQuantity) AS iPosition[/FONT]
[FONT=Arial]FROM tblTrades AS T[/FONT]
[FONT=Arial]GROUP BY T.sTicker[/FONT]
[FONT=Arial]HAVING sum(T.iQuantity)>0;[/FONT]
Note:
If sum(T.iQuantity) > 0 the trade is still open, i.e. the stock is part of the portfolio
If sum(T.iQuantity) = 0 the trade has been closed, i.e. the stock is no longer part of the portfolio
However, I struggle to formulate a query which gives me only the trades for the stocks which are in the portfolio currently (and have been part of the portfolio at a certain point in time in the past, respectively). That is, based on my sample data I would like to have the following result as per today:
Code:
[FONT=Arial]dtDate – sTicker – iQuantity – dPrice [/FONT]
[FONT=Arial]01/03/2011 – A – 10 – 50.00[/FONT]
[FONT=Arial]01/05/2011 – A – 10 – 55.00[/FONT]
[FONT=Arial]01/03/2011 – B – 10 – 25.00[/FONT]
[FONT=Arial]01/06/2011 – B – 10 – 20.00[/FONT]
[FONT=Arial]01/10/2011 – D – 50 – 5.00[/FONT]
[FONT=Arial]01/13/2011 – D – 25 – 6.00[/FONT]
…and for a certain date in the past (e.g. 01/04/2011):
Code:
[FONT=Arial]dtDate – sTicker – iQuantity – dPrice [/FONT]
[FONT=Arial]01/03/2011 – A – 10 – 50.00[/FONT]
[FONT=Arial]01/03/2011 – B – 10 – 25.00[/FONT]
[FONT=Arial]01/03/2011 – C – 15 – 5.00[/FONT]
Any ideas?
Thanks for your help –
JapanFreak
Attachments
Last edited: