473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Query (the last five visits to each spot)

Hi,

I'm having a problem with a query. I'm running Access 2000 and a table
with 35000 rows.

The table contains several visits to the spots with a CODE (spot code)
and DATE (visit date) columns. Each spot have 10 visits or more, so
the CODE is repeated in the column, an the respective DATE of that
visit, witch are not repeated. I would like to know the last five
visits to each spot. If it's possible.

Thanks in advance. Regards,

Eloi

Jun 14 '07 #1
2 2157
Hi Eloi,

Try something like this:

SELECT * FROM yourTbl t1 WHERE EXISTS
(SELECT * FROM (SELECT TOP 5 * FROM yourTbl t2 WHERE t2.code = t1.code
ORDER BY datefield desc) t3 WHERE t3.rowID = t1.rowID)

This sample requires that you have an autonumber column which would
uniquely identify each row. I named my autonumber column 'rowID'. Here
we use the Top clause to retrieve the top 5 rows for each code based on
the most recent dates. Example: code 'A' is listed for '1/1/07',
'1/2/07',...'1/10/07'

Select Top 5 * from yourTbl where code = 'A' Order By DateField Desc

This would return all the rows for code = 'A' for dates
1/6/07, 1/7/07, 1/8/07, 1/9/07, 1/10/07

We order the dates in Descending mode so that the first dates in the
return list are the most recent dates : 1/10/07 is more recent than
1/1/07. In the first sample above, I generalize this so that we can
retrieve the top 5 rows for each group of codes instead of just one
code. I use a subquery which I name t3, and I link the subquery to
yourTbl using the autonumber field 'rowID'
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Jun 14 '07 #2
Hi Rich,

Thanks for answering. I tried out your sql code and it didn't seem to
work, it returns an empty table. Here is the sql sentence I had used:

SELECT * FROM yourTbl AS t1
WHERE EXISTS (SELECT * FROM (SELECT TOP 5 * FROM yourTbl AS t2
WHERE t2.CODE = t1.CODE
ORDER BY DATEFIELD desc) t3 WHERE t3.ROWID = t1.ROWID)
;

Here is the first 30 row's from the table:

ROWID CODE DATEFIELD
1 OL0021 23/12/2004
2 OL0001 23/12/2004
3 OL0009 23/12/2004
4 OL0023 23/12/2004
5 OL0008 23/12/2004
6 OL0010 23/12/2004
7 OL0013 23/12/2004
8 OL0002 23/12/2004
9 OL0007 23/12/2004
10 OL0011 23/12/2004
11 OL0024 23/12/2004
12 OL0025 23/12/2004
13 OL0022 23/12/2004
14 OL0026 23/12/2004
15 OL0005 23/12/2004
16 OL0004 23/12/2004
17 OL0014 23/12/2004
18 OL0012 23/12/2004
19 OL0039 23/12/2004
20 OL0003 23/12/2004
21 OL0040 23/12/2004
22 OL0015 23/12/2004
23 OL0006 23/12/2004
24 OL0001 14/01/2005
25 OL0004 14/01/2005
26 OL0009 14/01/2005
27 OL0008 14/01/2005
28 OL0015 14/01/2005
29 OL0003 14/01/2005
30 OL0006 14/01/2005
|
|-this are repeated a lot more times with a different
date [DATEFIELD].

Do you know why it's not working?
Thanks again for wasting you time with this.

Regards,

Eloi

Jun 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Dave | last post by:
Hi all, I've been trying to figure this out for the last day and a half and it has me stumped. I've got a web application that I wrote that keeps track of trading cards I own, and I'm moving it...
2
by: Uri Lazar | last post by:
hi, im working on this for a long time. i'm using MSsql-server2000 i have a table that records users visits to rooms. the columns are room_id, user_id, visits. i want to write a query that can...
1
by: Mat | last post by:
The code below returns the number of Service Visits logged to Each Property. What I'd like to do is summarise this data by displaying the number of Properties with 1,2,3,4,n Visits. I assume...
16
by: Konrad Viltersten | last post by:
Suppose you got a really long page and you'd like to enable the user (supposedly, there's only one but if it's not to difficult we could extend that to any number) not to have to scroll to the...
3
by: Serious_Practitioner | last post by:
Good day, and thank you in advance for any assistance you can provide. I have a table in an Access 2000 .mdb file, and I've run into something odd and insolvable, at least for me. The database is...
3
by: torpecool | last post by:
Hello Everyone, I have been searching around for an answer to this question with no luck. I hope that some of you may have a couple of good ideas I could try. I am running MySQL 4.1.20. ...
9
by: DFS | last post by:
The following data set is building inspection visits. It consists of multiple visits (2+) made to the same building on the same day. I want to get a list of visits made to the same building on...
2
by: Dinesh | last post by:
Hi experts, I am working on SQL Server 2005. Now i have to write a query which will extract some information from a table. My main table is having few columns supose 3 columns. EmpID ...
0
Telinstryata
by: Telinstryata | last post by:
I have 3 tables for visits. "Visits" holds the visit information, "ReferredBy" holds the different types of referred-by info, and VisitRefByMap maps the two together in a many-to-many relationship. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.