matching records

G

Gary

I have created 3 tables with the exact same fields. The three tables are
Mens Sales, Women's Sales, Youth Sales. Each of the three tables has some
customers in it that are in the other tables. I need to create a fourth
table from the three so that I get all of the records that are unique so that
a get a full list of customers without duplicating them. The info that i
need is the email address of each customer without duplication in the new
table
 
B

Ben

Gary,

What I would do is create 4 queris, one for each of the Sales table like this:
Select emailaddress from women sales/ men sales/ youth sales

then a forth one that basically union them

select * from womensales
union
select * from mensales
union
select * from youthsales

The union will take out all the dups.

HTH,

Ben
 
K

ken

By having separate tables for each sale type you are encoding data as
column names. A fundamental principle of the database relational
model, 'the information principle' is that data is stored as explicit
values at column positions in rows in tables, and in no other way.
You should do two things:

1. Merge the tables into a single Sales table.

2. Then decompose the table into related Sales, SalesTypes and
Customers tables

To merge the tables firstly create a Sales table which matches the
structure of your existing tables, plus a text SaleType column and a
numeric CustomerID column. Then insert rows from your existing tables
by appending all columns fro each of the originals, along with a
constant for the SaleType in each case. So you'll need to execute
three append queries along these lines (simplified), firstly for Mens
sales:

INSERT INTO Sales(Customer, Item, Amount, SaleType)
SELECT Customer, Item, Amount, "Mens"
FROM [Mens Sales];

Do the same for womens sales, changing the constant to "Womens" and
the table name to [Womens Sales], and then again for Youth sales.

Next create Customers table with columns CustomerID (primary key) and
Customer, and fill this with the following append query:

INSERT INTO Customers
SELECT DISTINCT Customer
FROM Sales:

Now update the CustomerID column in Sales with:

UPDATE Sales INNER JOIN Customers
ON Sales.Customer = Customers.Customer
SET Sales.CustomerID = Customers.CustomerID;

Now delete the redundant Customer column from Sales.

Create a SaleTypes table with one Column SaleType (primary key) and
insert three rows into it with values Mens, Womens and Youth. Note
that these values must exactly match the values used as the constants
when inserting rows into the Sales table.

Finally create relationships between Customers and Sales on CustomerID
and between Sales and SaleTypes on SaleType, enforcing referential
integrity in each case. In the relationship between Sales and
SaleTypes also enforce cascade updates as 'natural' keys have been
used rather than numeric 'surrogate' keys as with the relationship
between Customers and Sales. The reason for using surrogate keys in
the latter case is that names can be duplicated, so two or more
customers could have the same name.

I've simplified things slightly above, so there may be more columns
which will need to be appended into Sales and Customers than I've
indicated, but hopefully you'll see the principles involved. Post
back if you need further clarification.

Ken Sheridan
Stafford, England
 
J

John W. Vinson

I have created 3 tables with the exact same fields. The three tables are
Mens Sales, Women's Sales, Youth Sales.

Then you made a very common mistake in database design. A sale is a sale is a
sale! You ahould have ONE sales table.
Each of the three tables has some
customers in it that are in the other tables.

And if you have all your customer information jumbled in with your sales
information you've made another mistake!
I need to create a fourth
table from the three so that I get all of the records that are unique so that
a get a full list of customers without duplicating them. The info that i
need is the email address of each customer without duplication in the new
table

You should take this good progress a step further and move to a normalized
database structure:

Customers
CustomerID <Primary Key>
LastName
FirstName
Sex
DOB <if you can persuade your customers to reveal their age>

Sales
SaleID <autonumber primary key>
CustomerID <link to Customers>
SaleDate <date/time>
<other info about the sale>

You may want to look at some of these resources, particularly one of the
tutorials:

Jeff Conrad's resources page:
http://www.accessmvp.com/JConrad/accessjunkie/resources.html

The Access Web resources page:
http://www.mvps.org/access/resources/index.html

A free tutorial written by Crystal (MS Access MVP):
http://allenbrowne.com/casu-22.html

MVP Allen Browne's tutorials:
http://allenbrowne.com/links.html#Tutorials
 

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

Top