473,830 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query to Select Maximum Age

Another basic question from newbie. I have a 10 million record database
of patient contact with their physician. The original file contained
all thier clinical information and personal characteristics . Each
patient record was repeated for each visit. I only want a table of
unique records for each patient with their characteristics , simple
enough with select distinct, but I found that I obtained duplicate
records if the patient visited their doctor between birthdays. I have
decided to take the record with their max age as more current. The
following however did not work, any suggestions?

SELECT DISTINCT ptnum, age, sex, address
FROM ptdb
GROUP BY ptnum, age, sex, address
HAVING (max(age));

Sep 7 '06 #1
3 4161
Try using a Group By query instead of using DISTINCT. Group By ptnum,
sex, address, MAX of age. That should give you one record per patient,
unless your data is screwy.

johnds wrote:
Another basic question from newbie. I have a 10 million record database
of patient contact with their physician. The original file contained
all thier clinical information and personal characteristics . Each
patient record was repeated for each visit. I only want a table of
unique records for each patient with their characteristics , simple
enough with select distinct, but I found that I obtained duplicate
records if the patient visited their doctor between birthdays. I have
decided to take the record with their max age as more current. The
following however did not work, any suggestions?

SELECT DISTINCT ptnum, age, sex, address
FROM ptdb
GROUP BY ptnum, age, sex, address
HAVING (max(age));
Sep 7 '06 #2
Running the query now, takes a very long time. max of age does not work
in the Group By clause, so the query running now is:
SELECT ptnum, age, sex, address
FROM ptdb
GROUP BY ptnum, age, sex, address
HAVING (max(age));

Should this work?

ManningFan wrote:
Try using a Group By query instead of using DISTINCT. Group By ptnum,
sex, address, MAX of age. That should give you one record per patient,
unless your data is screwy.

johnds wrote:
Another basic question from newbie. I have a 10 million record database
of patient contact with their physician. The original file contained
all thier clinical information and personal characteristics . Each
patient record was repeated for each visit. I only want a table of
unique records for each patient with their characteristics , simple
enough with select distinct, but I found that I obtained duplicate
records if the patient visited their doctor between birthdays. I have
decided to take the record with their max age as more current. The
following however did not work, any suggestions?

SELECT DISTINCT ptnum, age, sex, address
FROM ptdb
GROUP BY ptnum, age, sex, address
HAVING (max(age));
Sep 7 '06 #3
To close the loop on this one, the code does not work, data is fine. I
tried different ways, still got records with both ages. Eventually
exported to SPSS and used aggregate functions and max(age), worked
first time.
johnds wrote:
Running the query now, takes a very long time. max of age does not work
in the Group By clause, so the query running now is:
SELECT ptnum, age, sex, address
FROM ptdb
GROUP BY ptnum, age, sex, address
HAVING (max(age));

Should this work?

ManningFan wrote:
Try using a Group By query instead of using DISTINCT. Group By ptnum,
sex, address, MAX of age. That should give you one record per patient,
unless your data is screwy.

johnds wrote:
Another basic question from newbie. I have a 10 million record database
of patient contact with their physician. The original file contained
all thier clinical information and personal characteristics . Each
patient record was repeated for each visit. I only want a table of
unique records for each patient with their characteristics , simple
enough with select distinct, but I found that I obtained duplicate
records if the patient visited their doctor between birthdays. I have
decided to take the record with their max age as more current. The
following however did not work, any suggestions?
>
SELECT DISTINCT ptnum, age, sex, address
FROM ptdb
GROUP BY ptnum, age, sex, address
HAVING (max(age));
Sep 8 '06 #4

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

Similar topics

4
2661
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I need to be able to do a search for specific tickets withing price ranges, different locations within the theaters, etc. etc. My problem is in the search one of the criteria is to search for a group of seats together. For example let's say...
11
2147
by: 73blazer | last post by:
We are migrating a customer from Version 7.1 FP3, to Version 8.2 (8.1 FP8). For the most part, things are faster, but there is one query that is much much slower, and it is a query that is used all the time. select ATTR1,ATTR2,ATTR3,ATTR4 from physical.part_list where S_PART_NUMBER like '%KJS%' The widlcard before and after seems to be hosing it, but for this particular piece of the application, this type of query is neccessary.
4
2726
by: benignor | last post by:
Hi, I have a query that looks like this: PatientID LastVisit LastMed LastVaccine 1001 1/1/2004 3/1/2004 10/1/2004 1002 10/1/2005 3/1/2004 10/1/2004 ..... I need a calculated field (let's call it LastActivity) that contains
6
4854
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID SalesManName AT Alan Time
24
19920
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every renewal in the history of the policyholder. The information is in 2 tables, policy and customer, which share the custid data. The polno changes with every renewal Renewals in 2004 would be D, 2005 S, and 2006 L. polexpdates for a given customer...
3
5215
by: skennd | last post by:
Here's my problem in exact replication: I have used the find duplicate query in Access, and the query determined the following duplicate values by the following query: In (SELECT FROM As Tmp GROUP BY HAVING Count(*)>1 )
0
361
by: x taol | last post by:
i want to a query. SELECT MAX(fNum) FROM tTable WHERE fDate IN (SELECT MAX(fDate) FROM tTable WHERE fDate IN (SELECT fDate FROM tTable WHERE fDate IN (SELECT fDate FROM tTable WHERE (fRegion="Califo") AND (fProduct="G274") AND (fColor="Brown") AND (fDate<=#5/13/2006#)))) unfortunately, the upper query is not working in my hope. i want to following query.
0
326
by: Armando | last post by:
Hi there, I have two tables in access, one called "A", which has the following fields: VendorID VendorName DocumentNumber Amount Period 123 ABC 55545 132 1 456 DEF 55776 145 1 487 ADE 54758 100 1 123 ABC 57457 665 2
1
6236
by: Vinod Sadanandan | last post by:
A Roadmap To Query Tuning ============================ For each SQL statement, there are different approaches that could be used to retrieve the required data. Optimization is the process of choosing the most efficient way to retrieve this data based upon the evaluation of a number of different criteria. The CBO bases optimization choices on pre-gathered table and index statistics while the RBO makes it's decisions based on a set of ...
5
3701
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
After having played around with LINQ and reading the literature on it I have not been successful in getting this to work so I am looking for some guidance: I want to find a hunk in a collection whose Start field is the largest in the collection but less than an arbitrary cutoff ("selectedRow"). The result of the query should just be that single object of type Hunk. (No two objects will have the same value for their Start fields.) Here is...
0
9791
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10487
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10525
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10202
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5617
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3076
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.