A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

query to update familyID by last name



 
 
Thread Tools Display Modes
  #1  
Old March 18th, 2010, 07:21 PM posted to microsoft.public.access.queries
Kathy R.
external usenet poster
 
Posts: 20
Default query to update familyID by last name

I have a set of about 500 names that I need to break down into family
groups. The data was imported from an old database and this set of
names does not have any family association. Currently, for convenience
sake, they are all in the same "family," ie their FamID is all the same.

Table Structu
tblFamily
FamID (primary key)
FamLastName

tblIndividual
InID (primary key)
InFamID (foreign key)
FirstName
LastName

What I would like to do is to build a query, sorted on LastName where
all of the same last name would be dumped into a new FamID and have the
FamLastName in tblFamily and InFamID in tblIndividual updated. I've
looked at MIN and MAX, but that just gave me the first record (Abbott)
or the last record (Zyglocke).

Can this be done in a query, perhaps as a two-step process, or is it
more complicated than that? Your help would be very much appreciated!

By the way, I realize that all of the "Jones" may not belong in the same
family, but this will get me one step closer. The next step is to
compare them to the old, hand-written membership records.

Kathy R.
  #2  
Old March 18th, 2010, 11:22 PM posted to microsoft.public.access.queries
KARL DEWEY
external usenet poster
 
Posts: 10,767
Default query to update familyID by last name

Try this --
SELECT Q.LastName, (SELECT COUNT(*) FROM [tblIndividual] Q1
WHERE Q1.[LastName] Q.[LastName])+1 AS InFamID
FROM tblIndividual AS Q
ORDER BY Q.LastName;

--
Build a little, test a little.


"Kathy R." wrote:

I have a set of about 500 names that I need to break down into family
groups. The data was imported from an old database and this set of
names does not have any family association. Currently, for convenience
sake, they are all in the same "family," ie their FamID is all the same.

Table Structu
tblFamily
FamID (primary key)
FamLastName

tblIndividual
InID (primary key)
InFamID (foreign key)
FirstName
LastName

What I would like to do is to build a query, sorted on LastName where
all of the same last name would be dumped into a new FamID and have the
FamLastName in tblFamily and InFamID in tblIndividual updated. I've
looked at MIN and MAX, but that just gave me the first record (Abbott)
or the last record (Zyglocke).

Can this be done in a query, perhaps as a two-step process, or is it
more complicated than that? Your help would be very much appreciated!

By the way, I realize that all of the "Jones" may not belong in the same
family, but this will get me one step closer. The next step is to
compare them to the old, hand-written membership records.

Kathy R.
.

  #3  
Old March 19th, 2010, 12:23 AM posted to microsoft.public.access.queries
John Spencer
external usenet poster
 
Posts: 7,815
Default query to update familyID by last name

I would use two queries.

First query would populate tblFamily. FamID would be an autonumber field to
make this easy. If FamId is something else, then you need to describe what
field type and how the data for the field is constructed.

INSERT INTO tblFamily (famLastName)
SELECT DISTINCT FamLastName
FROM tblIndividual

Now, I would use an update query to populate the InFamId field

UPDATE tblIndividual INNER JOIN tblFamily
ON tblIndividual.FamLastName = tblFamily
SET tblIndividual.InFamID = [tblFamily].[FamID]

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Kathy R. wrote:
I have a set of about 500 names that I need to break down into family
groups. The data was imported from an old database and this set of
names does not have any family association. Currently, for convenience
sake, they are all in the same "family," ie their FamID is all the same.

Table Structu
tblFamily
FamID (primary key)
FamLastName

tblIndividual
InID (primary key)
InFamID (foreign key)
FirstName
LastName

What I would like to do is to build a query, sorted on LastName where
all of the same last name would be dumped into a new FamID and have the
FamLastName in tblFamily and InFamID in tblIndividual updated. I've
looked at MIN and MAX, but that just gave me the first record (Abbott)
or the last record (Zyglocke).

Can this be done in a query, perhaps as a two-step process, or is it
more complicated than that? Your help would be very much appreciated!

By the way, I realize that all of the "Jones" may not belong in the same
family, but this will get me one step closer. The next step is to
compare them to the old, hand-written membership records.

Kathy R.

  #4  
Old March 21st, 2010, 07:11 PM posted to microsoft.public.access.queries
Kathy R.[_3_]
external usenet poster
 
Posts: 25
Default query to update familyID by last name

Thank you Karl. I tried this, but it didn't do quite what I wanted. It
updated all of the InFamID's in the tblIndividual, grouped by last name,
but I need them separated into family (by last name) groups in the
tblFamily and then have those FamId's updated in the tblIndividual.

I ended up using John's solution below, but I've tucked this away in my
"should I ever need to do this in the future, here's how" folder. I can
learn as much by something that doesn't work as something that does!

Kathy R.

KARL DEWEY wrote:
Try this --
SELECT Q.LastName, (SELECT COUNT(*) FROM [tblIndividual] Q1
WHERE Q1.[LastName] Q.[LastName])+1 AS InFamID
FROM tblIndividual AS Q
ORDER BY Q.LastName;

  #5  
Old March 21st, 2010, 07:14 PM posted to microsoft.public.access.queries
Kathy R.[_3_]
external usenet poster
 
Posts: 25
Default query to update familyID by last name

Thank you John! With a little tweaking (I had to build a select query
first to choose just those families that weren't already in family
groups) this worked like a charm. The one thing I was missing was the
word "DISTINCT." If I had that I could have looked in the help file for
how to do it. It's like having a word on the tip of your tongue, but
not being able to bring it to mind.

Kathy R.

John Spencer wrote:
I would use two queries.

First query would populate tblFamily. FamID would be an autonumber
field to make this easy. If FamId is something else, then you need to
describe what field type and how the data for the field is constructed.

INSERT INTO tblFamily (famLastName)
SELECT DISTINCT FamLastName
FROM tblIndividual

Now, I would use an update query to populate the InFamId field

UPDATE tblIndividual INNER JOIN tblFamily
ON tblIndividual.FamLastName = tblFamily
SET tblIndividual.InFamID = [tblFamily].[FamID]

John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Kathy R. wrote:
I have a set of about 500 names that I need to break down into family
groups. The data was imported from an old database and this set of
names does not have any family association. Currently, for
convenience sake, they are all in the same "family," ie their FamID is
all the same.

Table Structu
tblFamily
FamID (primary key)
FamLastName

tblIndividual
InID (primary key)
InFamID (foreign key)
FirstName
LastName

What I would like to do is to build a query, sorted on LastName where
all of the same last name would be dumped into a new FamID and have
the FamLastName in tblFamily and InFamID in tblIndividual updated.
I've looked at MIN and MAX, but that just gave me the first record
(Abbott) or the last record (Zyglocke).

Can this be done in a query, perhaps as a two-step process, or is it
more complicated than that? Your help would be very much appreciated!

By the way, I realize that all of the "Jones" may not belong in the
same family, but this will get me one step closer. The next step is
to compare them to the old, hand-written membership records.

Kathy R.

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 07:36 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.