Remove multiple entries for the same person in a query

  • Thread starter Thread starter Bob Richardson
  • Start date Start date
B

Bob Richardson

I have a Contact file and a Class file (which includes a Contact key for the
teacher). I want to print a label for each teacher so my query between these
to tables is very straight forward. However, if a teacher teaches two
classes, they end up in the query twice, and I don't want to print two
labels for the same person. How can this be done?
 
Sounds like you need to add a DISTINCT keyword to your query. Please post
your query here and we'll give you some advice on modifying it.
 
The query isn't a SQL query, its an access query. The two tables (Contact,
Classes) are joined. My output gives me one record for every record in the
Classes file - which results in the same person showing up multiple times
when he teaches two or more classes.

It seems that the DISTINCT feature might work, but how to implement it.

I have to eliminate the duplicates either in the query, OR the report that
produces the labels.
 
Bob Richardson said:
The query isn't a SQL query, its an access query. The two tables (Contact,
Classes) are joined. My output gives me one record for every record in the
Classes file - which results in the same person showing up multiple times
when he teaches two or more classes.

It seems that the DISTINCT feature might work, but how to implement it.

I have to eliminate the duplicates either in the query, OR the report that
produces the labels.

SQL is a language that is common to both SQL Server and Access. There are
some differences between the two implementations but there is far more in
common than different. The DISTINCT qualifier is common to both. As Ted
stated earlier, it's difficult to provide much help without seeing the SQL
that your working with.

"Select PersonName from Person"

would return a list of names from the Person table, but it would include any
duplicates that existed.

"Select DISTINCT PersonName from Person"

would return a list, but each name would be listed only once.

HTH,
Randy Harris
 
Randy Harris said:
SQL is a language that is common to both SQL Server and Access. There are
some differences between the two implementations but there is far more in
common than different. The DISTINCT qualifier is common to both. As Ted
stated earlier, it's difficult to provide much help without seeing the SQL
that your working with.

"Select PersonName from Person"

would return a list of names from the Person table, but it would include
any
duplicates that existed.

"Select DISTINCT PersonName from Person"

would return a list, but each name would be listed only once.

HTH,
Randy Harris

Thanks Randy. Where would I use a SQL statement to define my data in a
report, or in a query? I don't see a spot for that.

I tried using a TOtal query, with this definition

Name: First([Contacts.FirstName] & " " & [Contacts.LastName])

as an expression.

It almost worked. FOr some reason it also included the very first person in
the file, although she isn't in the selected list of classes ?
 
Bob Richardson said:
Randy Harris said:
SQL is a language that is common to both SQL Server and Access. There are
some differences between the two implementations but there is far more in
common than different. The DISTINCT qualifier is common to both. As Ted
stated earlier, it's difficult to provide much help without seeing the SQL
that your working with.

"Select PersonName from Person"

would return a list of names from the Person table, but it would include
any
duplicates that existed.

"Select DISTINCT PersonName from Person"

would return a list, but each name would be listed only once.

HTH,
Randy Harris

Thanks Randy. Where would I use a SQL statement to define my data in a
report, or in a query? I don't see a spot for that.

I tried using a TOtal query, with this definition

Name: First([Contacts.FirstName] & " " & [Contacts.LastName])

as an expression.

It almost worked. FOr some reason it also included the very first person in
the file, although she isn't in the selected list of classes ?

Like always, there's more than one way to do it. In most cases, you will
create the query within the definition of the report. As you design the
report, open the parameter window and find RecordSource. When you click on
the button for RecordSource it will open the query builder. Use the query
builder to retrieve whatever data is required for the report.

Another method is to simply create a saved query. You can use the query
builder to confirm that you are getting the data that you want to appear in
the report, then make that query the RecordSource for your report.

You still haven't provided a lot of information about what you are trying to
accomplish. Any of the Aggregate queries (including Total) will permit you
to "Group By" whichever field or fields you wish to. The Group By will
prevent the record from appearing more than once in the output.

Randy
 
Bob Richardson said:
There are

Thanks Randy. Where would I use a SQL statement to define my data in a
report, or in a query? I don't see a spot for that.


Bob Richardson,

When viewing a Query in Design View, you can go to the menus: View >
SQL View.

This will show you the SQL of the query.


Notes:

Access takes care of most business by using SQL. Part of what it
does it hide the SQL to make the job of using a database easier for
beginners. When you make a query in Design View, "under the hood"
Access is assembling an SQL statement that actually accomplishes the
work.

You can't "post" a Query's Design View here on Usenet newsgroups.
So, when asking questions about Queries, or receiving advice about
them, it has to be done with SQL statements that are copied and
pasted back and forth between a Query's SQL View and newsgroup
posts.


Sincerely,

Chris O.
 
Randy Harris said:
Bob Richardson said:
Randy Harris said:
"Bob Richardson" <bobr at whidbey dot com> wrote in message
The query isn't a SQL query, its an access query. The two tables
(Contact,
Classes) are joined. My output gives me one record for every record in
the
Classes file - which results in the same person showing up multiple times
when he teaches two or more classes.

It seems that the DISTINCT feature might work, but how to implement
it.

I have to eliminate the duplicates either in the query, OR the report
that
produces the labels.

Sounds like you need to add a DISTINCT keyword to your query. Please
post
your query here and we'll give you some advice on modifying it.
--
-Ted



SQL is a language that is common to both SQL Server and Access. There are
some differences between the two implementations but there is far more in
common than different. The DISTINCT qualifier is common to both. As Ted
stated earlier, it's difficult to provide much help without seeing the SQL
that your working with.

"Select PersonName from Person"

would return a list of names from the Person table, but it would
include
any
duplicates that existed.

"Select DISTINCT PersonName from Person"

would return a list, but each name would be listed only once.

HTH,
Randy Harris

Thanks Randy. Where would I use a SQL statement to define my data in a
report, or in a query? I don't see a spot for that.

I tried using a TOtal query, with this definition

Name: First([Contacts.FirstName] & " " & [Contacts.LastName])

as an expression.

It almost worked. FOr some reason it also included the very first person in
the file, although she isn't in the selected list of classes ?

Like always, there's more than one way to do it. In most cases, you will
create the query within the definition of the report. As you design the
report, open the parameter window and find RecordSource. When you click on
the button for RecordSource it will open the query builder. Use the query
builder to retrieve whatever data is required for the report.

Another method is to simply create a saved query. You can use the query
builder to confirm that you are getting the data that you want to appear
in
the report, then make that query the RecordSource for your report.

You still haven't provided a lot of information about what you are trying
to
accomplish. Any of the Aggregate queries (including Total) will permit you
to "Group By" whichever field or fields you wish to. The Group By will
prevent the record from appearing more than once in the output.

Randy

Thanks Randy - I didn't know about SQL View. Here's the SQL

SELECT Contacts.Zip, Contacts.Title, First([Contacts.FirstName] & " " &
[Contacts.LastName]) AS Name, Contacts.Suffix, Q_Classes.EvCode,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State,
Contacts.LastName, Contacts.FirstName, Contacts.Title, Contacts.Suffix,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State
FROM Contacts INNER JOIN Q_Classes ON Contacts.PersonID =
Q_Classes.PresenterID
GROUP BY Contacts.Zip, Contacts.Title, Contacts.Suffix, Q_Classes.EvCode,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State,
Contacts.LastName, Contacts.FirstName, Contacts.Title, Contacts.Suffix,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State
HAVING (((Contacts.Zip)<>"") AND
((Q_Classes.EvCode)=[Forms]![EventForm]![Event]))
ORDER BY Contacts.LastName, Contacts.FirstName;

This correctly gets me all the Names for the Presenters who have been
assigned to a class in the EvCode...EXCEPT it also gives me one extra
record...the first name alphabetically with a non blank zip code.
 
Try testing for Contacts.ZIP is NOT Null vice <>""

Also, you could probably speed this up by changing from a HAVING Clause to a
WHERE Clause.

SELECT Contacts.Zip, Contacts.Title, First([Contacts.FirstName] & " " &
[Contacts.LastName]) AS Name, Contacts.Suffix, Q_Classes.EvCode,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State,
Contacts.LastName, Contacts.FirstName, Contacts.Title, Contacts.Suffix,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State
FROM Contacts INNER JOIN Q_Classes ON Contacts.PersonID =
Q_Classes.PresenterID
WHERE (((Contacts.Zip) IS NOT NULL) AND
((Q_Classes.EvCode)=[Forms]![EventForm]![Event]))
GROUP BY Contacts.Zip, Contacts.Title, Contacts.Suffix, Q_Classes.EvCode,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State,
Contacts.LastName, Contacts.FirstName, Contacts.Title, Contacts.Suffix,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State
ORDER BY Contacts.LastName, Contacts.FirstName;

If you are using the query grid, you would need to add Contacts.ZIP to the query
a second time and change the second instance from GROUP BY to WHERE and change
Q_Classes.PresenterID to WHERE.

You could also do this with a query like the following which uses a sub-query to
identify the presenteriIDs that are to be matched.

SELECT Contacts.Zip, Contacts.Title
, [Contacts.FirstName] & " " & [Contacts.LastName]) AS Name
, Contacts.Suffix, Q_Classes.EvCode,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State,
Contacts.LastName, Contacts.FirstName, Contacts.Title, Contacts.Suffix,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State
FROM Contacts
WHERE Contacts.ZIP is NOT NULL
AND Contacts.PersonID IN (
SELECT PresenterID
FROM q_Classes
WHERE EvCode=[Forms]![EventForm]![Event])

In the query grid, you would build the select query on contacts table and then
under the PersonID field you would insert in the criteria:

IN (SELECT PresenterID FROM q_Classes WHERE EvCode=[Forms]![EventForm]![Event])

Uncheck the SHOW box for the PersonId field

Bob said:
Randy Harris said:
Bob Richardson said:
"Bob Richardson" <bobr at whidbey dot com> wrote in message
The query isn't a SQL query, its an access query. The two tables
(Contact,
Classes) are joined. My output gives me one record for every record in
the
Classes file - which results in the same person showing up multiple times
when he teaches two or more classes.

It seems that the DISTINCT feature might work, but how to implement
it.

I have to eliminate the duplicates either in the query, OR the report
that
produces the labels.

Sounds like you need to add a DISTINCT keyword to your query. Please
post
your query here and we'll give you some advice on modifying it.
--
-Ted



SQL is a language that is common to both SQL Server and Access. There are
some differences between the two implementations but there is far more in
common than different. The DISTINCT qualifier is common to both. As Ted
stated earlier, it's difficult to provide much help without seeing the SQL
that your working with.

"Select PersonName from Person"

would return a list of names from the Person table, but it would
include
any
duplicates that existed.

"Select DISTINCT PersonName from Person"

would return a list, but each name would be listed only once.

HTH,
Randy Harris

Thanks Randy. Where would I use a SQL statement to define my data in a
report, or in a query? I don't see a spot for that.

I tried using a TOtal query, with this definition

Name: First([Contacts.FirstName] & " " & [Contacts.LastName])

as an expression.

It almost worked. FOr some reason it also included the very first person in
the file, although she isn't in the selected list of classes ?

Like always, there's more than one way to do it. In most cases, you will
create the query within the definition of the report. As you design the
report, open the parameter window and find RecordSource. When you click on
the button for RecordSource it will open the query builder. Use the query
builder to retrieve whatever data is required for the report.

Another method is to simply create a saved query. You can use the query
builder to confirm that you are getting the data that you want to appear
in
the report, then make that query the RecordSource for your report.

You still haven't provided a lot of information about what you are trying
to
accomplish. Any of the Aggregate queries (including Total) will permit you
to "Group By" whichever field or fields you wish to. The Group By will
prevent the record from appearing more than once in the output.

Randy

Thanks Randy - I didn't know about SQL View. Here's the SQL

SELECT Contacts.Zip, Contacts.Title, First([Contacts.FirstName] & " " &
[Contacts.LastName]) AS Name, Contacts.Suffix, Q_Classes.EvCode,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State,
Contacts.LastName, Contacts.FirstName, Contacts.Title, Contacts.Suffix,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State
FROM Contacts INNER JOIN Q_Classes ON Contacts.PersonID =
Q_Classes.PresenterID
GROUP BY Contacts.Zip, Contacts.Title, Contacts.Suffix, Q_Classes.EvCode,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State,
Contacts.LastName, Contacts.FirstName, Contacts.Title, Contacts.Suffix,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State
HAVING (((Contacts.Zip)<>"") AND
((Q_Classes.EvCode)=[Forms]![EventForm]![Event]))
ORDER BY Contacts.LastName, Contacts.FirstName;

This correctly gets me all the Names for the Presenters who have been
assigned to a class in the EvCode...EXCEPT it also gives me one extra
record...the first name alphabetically with a non blank zip code.
 
Thanks for your help John. I learned a lot more about queries in Access.
I've got it working now.

John Spencer said:
Try testing for Contacts.ZIP is NOT Null vice <>""

Also, you could probably speed this up by changing from a HAVING Clause to
a
WHERE Clause.

SELECT Contacts.Zip, Contacts.Title, First([Contacts.FirstName] & " " &
[Contacts.LastName]) AS Name, Contacts.Suffix, Q_Classes.EvCode,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State,
Contacts.LastName, Contacts.FirstName, Contacts.Title, Contacts.Suffix,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State
FROM Contacts INNER JOIN Q_Classes ON Contacts.PersonID =
Q_Classes.PresenterID
WHERE (((Contacts.Zip) IS NOT NULL) AND
((Q_Classes.EvCode)=[Forms]![EventForm]![Event]))
GROUP BY Contacts.Zip, Contacts.Title, Contacts.Suffix, Q_Classes.EvCode,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State,
Contacts.LastName, Contacts.FirstName, Contacts.Title, Contacts.Suffix,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State
ORDER BY Contacts.LastName, Contacts.FirstName;

If you are using the query grid, you would need to add Contacts.ZIP to the
query
a second time and change the second instance from GROUP BY to WHERE and
change
Q_Classes.PresenterID to WHERE.

You could also do this with a query like the following which uses a
sub-query to
identify the presenteriIDs that are to be matched.

SELECT Contacts.Zip, Contacts.Title
, [Contacts.FirstName] & " " & [Contacts.LastName]) AS Name
, Contacts.Suffix, Q_Classes.EvCode,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State,
Contacts.LastName, Contacts.FirstName, Contacts.Title, Contacts.Suffix,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State
FROM Contacts
WHERE Contacts.ZIP is NOT NULL
AND Contacts.PersonID IN (
SELECT PresenterID
FROM q_Classes
WHERE EvCode=[Forms]![EventForm]![Event])

In the query grid, you would build the select query on contacts table and
then
under the PersonID field you would insert in the criteria:

IN (SELECT PresenterID FROM q_Classes WHERE
EvCode=[Forms]![EventForm]![Event])

Uncheck the SHOW box for the PersonId field

Bob said:
Randy Harris said:
"Bob Richardson" <bobr at whidbey dot com> wrote in message


"Bob Richardson" <bobr at whidbey dot com> wrote in message
The query isn't a SQL query, its an access query. The two tables
(Contact,
Classes) are joined. My output gives me one record for every record
in
the
Classes file - which results in the same person showing up multiple
times
when he teaches two or more classes.

It seems that the DISTINCT feature might work, but how to implement
it.

I have to eliminate the duplicates either in the query, OR the
report
that
produces the labels.

Sounds like you need to add a DISTINCT keyword to your query.
Please
post
your query here and we'll give you some advice on modifying it.
--
-Ted



SQL is a language that is common to both SQL Server and Access.
There
are
some differences between the two implementations but there is far
more
in
common than different. The DISTINCT qualifier is common to both. As
Ted
stated earlier, it's difficult to provide much help without seeing
the
SQL
that your working with.

"Select PersonName from Person"

would return a list of names from the Person table, but it would
include
any
duplicates that existed.

"Select DISTINCT PersonName from Person"

would return a list, but each name would be listed only once.

HTH,
Randy Harris

Thanks Randy. Where would I use a SQL statement to define my data in
a
report, or in a query? I don't see a spot for that.

I tried using a TOtal query, with this definition

Name: First([Contacts.FirstName] & " " & [Contacts.LastName])

as an expression.

It almost worked. FOr some reason it also included the very first
person
in
the file, although she isn't in the selected list of classes ?


Like always, there's more than one way to do it. In most cases, you
will
create the query within the definition of the report. As you design the
report, open the parameter window and find RecordSource. When you click
on
the button for RecordSource it will open the query builder. Use the
query
builder to retrieve whatever data is required for the report.

Another method is to simply create a saved query. You can use the query
builder to confirm that you are getting the data that you want to
appear
in
the report, then make that query the RecordSource for your report.

You still haven't provided a lot of information about what you are
trying
to
accomplish. Any of the Aggregate queries (including Total) will permit
you
to "Group By" whichever field or fields you wish to. The Group By will
prevent the record from appearing more than once in the output.

Randy

Thanks Randy - I didn't know about SQL View. Here's the SQL

SELECT Contacts.Zip, Contacts.Title, First([Contacts.FirstName] & " " &
[Contacts.LastName]) AS Name, Contacts.Suffix, Q_Classes.EvCode,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State,
Contacts.LastName, Contacts.FirstName, Contacts.Title, Contacts.Suffix,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State
FROM Contacts INNER JOIN Q_Classes ON Contacts.PersonID =
Q_Classes.PresenterID
GROUP BY Contacts.Zip, Contacts.Title, Contacts.Suffix, Q_Classes.EvCode,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State,
Contacts.LastName, Contacts.FirstName, Contacts.Title, Contacts.Suffix,
Contacts.Address1, Contacts.Address2, Contacts.City, Contacts.State
HAVING (((Contacts.Zip)<>"") AND
((Q_Classes.EvCode)=[Forms]![EventForm]![Event]))
ORDER BY Contacts.LastName, Contacts.FirstName;

This correctly gets me all the Names for the Presenters who have been
assigned to a class in the EvCode...EXCEPT it also gives me one extra
record...the first name alphabetically with a non blank zip code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top