View Full Version : Date Query


TheJudge
11-11-2007, 10:14 AM
Can someone please help?

My access database holds the last 6 months stock market data i.e. close, high, low price for each stock. However, I want to show the results in Excel but only the last three values for each stock (as I have over 1000 stocks) so want to create a query in Access.

For example:

qDate qTicker qOp qHi qLo qCl qVo
08/11/2006 AAT.L 98 100 98 99 2361001
09/11/2006 AAT.L 98 98 98 98 378001
10/11/2006 AAT.L 99 99 97 97 66001

08/11/2006 VOD.L 198 100 198 199 2361020
09/11/2006 VOD.L 198 198 198 198 378020
10/11/2006 VOD.L 199 199 197 197 66010

08/11/2006 ARM.L 298 200 298 299 1236100
09/11/2006 ARM.L 298 298 298 298 317800
10/11/2006 ARM.L 299 299 297 297 66100

Any thoughts?

Thanks!

pbaldy
11-11-2007, 10:17 AM
This may work for you:

http://support.microsoft.com/kb/210039/en-us

TheJudge
11-11-2007, 11:15 AM
Thanks for the reply but having problems getting this to work - do you have an example?

pbaldy
11-11-2007, 11:17 AM
Actually I just used that technique on a production database, but the query is in SQL Server. Perhaps if you posted your db, someone could fix it for you.

TheJudge
11-11-2007, 11:42 AM
The forum won't let me upload the file - but the field names are above and this is my SQL:

In (Select Top 3 [qTicker] From Quotes Where
[qTicker]=[qDate].[qTicker] Order By [qTicker] Desc)

TheJudge
11-11-2007, 11:48 AM
picture attached.

TheJudge
11-12-2007, 04:24 AM
Can anyone please help?

Jon K
11-12-2007, 05:04 AM
Try this query.

SELECT Quotes.*
FROM Quotes
WHERE qDate In (Select Top 3 qDate from Quotes as S where S.qTicker=Quotes.qTicker order by qDate Desc)


Note: Running a subquery in a correlated way is inefficient and will take time if the table is large.
.

TheJudge
11-13-2007, 12:42 PM
Jon K - thanks very much that worked!

TheJudge
11-21-2007, 10:12 AM
Hi,

Using this query how do I display records with 'only' today's date?

SELECT Quotes.*
FROM Quotes
WHERE qDate In (Select Top 3 qDate from Quotes as S where S.qTicker=Quotes.qTicker order by qDate Desc)

Newbie100
11-21-2007, 02:38 PM
SELECT Quotes.*
FROM Quotes
WHERE qDate = Date()

TheJudge
11-25-2007, 10:42 AM
Thanks but I need to include this "WHERE qDate In (Select Top 3 qDate from Quotes as S where S.qTicker=Quotes.qTicker order by qDate Desc)" in the SQL query but show only today's records.

Any thoughts?

Rabbie
11-25-2007, 01:35 PM
Thanks but I need to include this "WHERE qDate In (Select Top 3 qDate from Quotes as S where S.qTicker=Quotes.qTicker order by qDate Desc)" in the SQL query but show only today's records.

Any thoughts?

Try

WHERE qDate In (Select Top 3 qDate from Quotes as S where S.qTicker=Quotes.qTicker and qDate = date() )

TheJudge
11-26-2007, 12:39 PM
This didn't work.

TheJudge
11-28-2007, 05:48 AM
Can anyone help please?

GUIDO22
11-28-2007, 06:17 AM
Is qDate a string date or 'Date' data type?

You need to ensure that the dates stored in your table are date strings only
ie. no timepstamp. Then the suggested code may work..... can you confirm no timestamp?