473,499 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

most recent records

Hi

I have a patient table with patient names and visiting dates. I want to
select those patients who have visited me at least 6 times and show the
dates that they visited ordered by name. I have managed this by
creating another table but Im sure it can be done with a couple of
queries.

thanks,Shumit

Oct 21 '06 #1
9 1707
sh****@eskatongardens.com wrote:
Hi

I have a patient table with patient names and visiting dates. I want to
select those patients who have visited me at least 6 times and show the
dates that they visited ordered by name. I have managed this by
creating another table but Im sure it can be done with a couple of
queries.

thanks,Shumit
You could create a column in your query that stores the counts using
Dcount(). Ex:
VisitCount : NZ(Dcount("PatientID","Patients","PatientID = " &
[PatientID]),0)

It's possible you have a patient name but havent entered the visit
data...thus I added the NZ to force a 0 to be displayed.
Oct 21 '06 #2
sh****@eskatongardens.com wrote:
Hi

I have a patient table with patient names and visiting dates. I want to
select those patients who have visited me at least 6 times and show the
dates that they visited ordered by name. I have managed this by
creating another table but Im sure it can be done with a couple of
queries.

thanks,Shumit
If all the information is in one table, this should do it:

SELECT PatientName, Visit
FROM Patients P1
WHERE P1.PatientName IN
(SELECT PatientName
FROM Patients
GROUP BY PatientName
HAVING COUNT(Visit) >= 6)
ORDER BY PatientName, Visit;

As an aside, if you are in fact storing patient information (name, DOB,
gender, etc) in the same table as visit information (date of visit,
reason, charges, etc.) it should be said this is generally considered
poor database design, and you think about normalizing the data structure.

Let us know if you have any questions.
--
Smartin
Oct 21 '06 #3
Take a look at this:

http://groups.google.com/group/comp....frm/thread/e11
90eba19e4e62f/4671fb0f3a8758e9?lnk=gst&q=aa+arens&rnum=1#4671fb0 f3a8758e9

Smartin wrote:
sh****@eskatongardens.com wrote:
Hi

I have a patient table with patient names and visiting dates. I want to
select those patients who have visited me at least 6 times and show the
dates that they visited ordered by name. I have managed this by
creating another table but Im sure it can be done with a couple of
queries.

thanks,Shumit

If all the information is in one table, this should do it:

SELECT PatientName, Visit
FROM Patients P1
WHERE P1.PatientName IN
(SELECT PatientName
FROM Patients
GROUP BY PatientName
HAVING COUNT(Visit) >= 6)
ORDER BY PatientName, Visit;

As an aside, if you are in fact storing patient information (name, DOB,
gender, etc) in the same table as visit information (date of visit,
reason, charges, etc.) it should be said this is generally considered
poor database design, and you think about normalizing the data structure.

Let us know if you have any questions.
--
Smartin
Oct 22 '06 #4
If all the information is in one table, this should do it:

SELECT PatientName, Visit
FROM Patients P1
WHERE P1.PatientName IN
(SELECT PatientName
FROM Patients
GROUP BY PatientName
HAVING COUNT(Visit) >= 6)
ORDER BY PatientName, Visit;

As an aside, if you are in fact storing patient information (name, DOB,
gender, etc) in the same table as visit information (date of visit,
reason, charges, etc.) it should be said this is generally considered
poor database design, and you think about normalizing the data structure.
Thanks for your reply but I omitted to state that I only want the last
6 records for each patient. My tables are separate, I am using
patientID to link between the visits table and the patients table,

thanks,Shumit

Oct 23 '06 #5
sh****@eskatongardens.com wrote:
>If all the information is in one table, this should do it:

SELECT PatientName, Visit
FROM Patients P1
WHERE P1.PatientName IN
(SELECT PatientName
FROM Patients
GROUP BY PatientName
HAVING COUNT(Visit) >= 6)
ORDER BY PatientName, Visit;

As an aside, if you are in fact storing patient information (name, DOB,
gender, etc) in the same table as visit information (date of visit,
reason, charges, etc.) it should be said this is generally considered
poor database design, and you think about normalizing the data structure.

Thanks for your reply but I omitted to state that I only want the last
6 records for each patient. My tables are separate, I am using
patientID to link between the visits table and the patients table,

thanks,Shumit
OK I am not the best at subqueries, but I will work on this because I
love this sort of pain.

I hope no one has a textbook answer (^:

--
Smartin
Oct 24 '06 #6
>
OK I am not the best at subqueries, but I will work on this because I
love this sort of pain.

I hope no one has a textbook answer (^:

--
Smartin
Its been a week now - This must be pretty painful !

Shumit

Nov 2 '06 #7
sh****@eskatongardens.com wrote:
>OK I am not the best at subqueries, but I will work on this because I
love this sort of pain.

I hope no one has a textbook answer (^:

--
Smartin

Its been a week now - This must be pretty painful !

Shumit
Yes, if you could send something for the pain, and the swelling too LOL...

Actually, I kinda of track of this. I'll put it back on my to-do list.

--
Smartin
Nov 3 '06 #8
sh****@eskatongardens.com wrote:
>OK I am not the best at subqueries, but I will work on this because I
love this sort of pain.

I hope no one has a textbook answer (^:

--
Smartin

Its been a week now - This must be pretty painful !

Shumit
OK, here you go. I hope you can use this.

Explanation follows SQL:
SELECT V1.VisitID, V1.PatientID, V1.VisitDate
FROM Visits AS V1

WHERE V1.VisitDate IN
(SELECT TOP 6 V2.VisitDate
FROM Visits AS V2
WHERE V1.PatientID = V2.PatientID
ORDER BY V2.VisitDate DESC)

AND V1.PatientID IN
(SELECT V3.PatientID
FROM Visits AS V3
GROUP BY V3.PatientID
HAVING COUNT (V3.VisitID) >=6)

ORDER BY PatientID, VisitDate DESC;
The Visits table consists of PatientID (which you can join to your
Patients table for more patient details) and VisitDate. VisitID is
totally optional here. I include it to demonstrate the uniqueness of
returned rows.

The first subquery limits the returned rows to the 6 most recent visits
per patient, however it allows for patients that have fewer than 6 visits.

Caveat: If there are ties on VisitDate within the top 6 you may get more
than 6 rows returned per patient. TOP works that way...

The second subquery further limits the returned rows to only include
those patients that have 6 or more visits.

This runs in < 1 sec against my table of 5000 records, YMMV. There may
be other solutions.

Regards,

--
Smartin
Nov 3 '06 #9
I am in awe!

Thanks,Shumit

Nov 6 '06 #10

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

Similar topics

7
2575
by: Nova's Taylor | last post by:
Hi folks, I am a newbie to Python and am hoping that someone can get me started on a log parser that I am trying to write. The log is an ASCII file that contains a process identifier (PID),...
2
2522
by: Randy Jackson | last post by:
First of all, I apologize in advance if this is covered somewhere in a FAQ. I did a Google search, but really couldn't find anything. I'm having a problem with a simple select query. I've got...
3
1359
by: TJM | last post by:
hello, I have a table which contains the audit time field. This audit time field records the current date and time when a record is inserted. I would like to output the most recent top 500 rows....
0
1161
by: Gary | last post by:
Does anyone know how to do a sort to find the most recent distinct records. We have records with equipment, and activities the equipment was used for, but I need a distinct list of where the...
1
3419
by: Tim Graichen | last post by:
Good morning, I have a sub-form that displays records from a table as a continuous form. The table has several hundred records, but the subform only displays five or six records. The records do...
2
3863
by: Shaiguy | last post by:
I have a table containing the following fields: ProjectUpdateID (PrimaryKey) ProjectID UpdateDate I would like to create a Query in Ms Access 2000 which will return me the most recent 2...
3
1849
by: manny | last post by:
Problem: how to have query show only most recent records. This query shows all exams in 2005 for particular individual (grades not shown to avoid embarrassing John Slacker!): SELECT...
4
2734
by: Sector 7G | last post by:
I'm working with a SQL query for a Human Resources database. Its intended purpose is to find all the paycheck records with a check date (prckhist.chkdate ) more recent than eleven days past the...
2
3276
by: robert.waters | last post by:
I need to perform the following: - select the most recent X number of records in a table (there is a timestamp field) - select the Nth occurrence of X number of records ex: - most recent 10...
13
3996
by: angi35 | last post by:
Hi - working in Access 2000... my goal is to combine fields from two tables in a query: Table 1 has ItemNumber and ItemDescription. There's only one record per ItemNumber. Table 2 has ItemAlias....
0
7130
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
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7171
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
7220
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...
1
6893
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...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.