Joining Tables

  • Thread starter Thread starter Jacob Frankham
  • Start date Start date
J

Jacob Frankham

Hi

I need to join two tables as follows (I currently have no primary keys
defined):

tbl_AllAudits (contract, office, x, y, z)
tbl_Audit (contract, office, a, b, c)

(A contract may have many offices and an office may have many contracts)
I want to add contract and office from tbl_Audit to tbl_AllAudits WITHOUT
duplicates

So, if (Lon02332, London) exists in tbl_AllAudits, I want data like
(Lon02332, Exeter) and (Dar99887, London) to be added to tbl_AllAudits but
NOT another (Lon02332, London).

ie only ONE COMBINATION of (contract, Office) may exist in tbl_AllAudits.

Would anyone be able to help me with the SQL on this, and/or query design?

Yours hopefully

Jake !
 
Not sure how this is "Join" if you are trying to update
on table with info from another. You can set a Unique
Index in the tbl_AllAudits with Contract and Office
together and set the Unique Property to Yes. This will
not allow any duplicate records to be added to the table
if there are records already in the table with those same
values in the record.

Drew
 
Sorry Drew

I didn't understand any of that, I am a bit of a novice

I don't think I can set office and contract as indexes because they may not
necessarily be involved in the table, ie some office may not hold contracts.

Help !

Jake
 
Your table setup is veryu confusing for something so
simple. Could you please tell me what information each
table is supposed to hold. Simply line out the process.
Are you storing Audits in the Audit table then moving
them over to the All Audits table when an action takes
place?? I will try to step down the "lingo" to assist.

Drew
 
Thanks for your help again Drew.

OK, this is how it goes:

I have a table which holds data on audits which have been carried out on
different offices. (I do not have any unique / primary keys keys set as
yet.)

There are 2 'types' of audit, 'Financial' and 'Performance' - the former
will have an associated 'contract' number (it is the contract that is
audited), the latter is recorded by the 'dept' whose performance has been
audited (it is the department that is audited).

The table structure is as follows:

tbl_AllAudits (region, office, contract, dept, auditType, risk, fundsAtRisk,
auditDate)

eg for a 'Financial' audit ("North", "Newcastle", "NTN001401", Null,
"Financial", "Low", £2,387, 20/07/2003)
and for a 'Performance' audit ("North", "Newcastle", Null, "Sales",
"Performance", "High", Null, 21/07/2003)

The other table holds office and contract/dept data for ALL of the offices
and contracts/depts that exist ie including those not-yet audited, thus:

tbl_OfficeLookup (office, contract, dept)

eg ("Newcastle", "NTN001401", Null)
("Newcastle", "NTN001222", Null)
("Newcastle", Null, "Sales")
("Newcastle", Null, "Admin")
etc

SO:

What I need to do is write 2 queries (one for Financial and one for
Performance) which selects the offices and contracts (or dept) where an
audit has NOT been done (ie an audit is required).
ie I somehow need to get this data together so I can :
(For Financial) - Select those offices and contracts which need auditing eg
Where auditDate is Null
(For Quality) - Select those offices and QualityTypes which need auditing eg
Where auditDate is Null

I hope this is enough background info for you to get a grasp of what I need
to do, and I really appreciate your time in reading all of this.

Hope you can help !!!

Cheer mate

Jake
 
First and foremost, I hope you have tables set up for
each of the "constant" type of values. You should have
tables for region, office (office should have a region
ID) so you would only have to enter an office in your
table and you would know (through relational database
structure) anything you wished to know about a region
(given an office does not belong to more than one region,
but that too could be made to work), Risk, AuditType and
Department. I would even make a table to input Contract
Numbers and then relate the contract number ID. This
will keep typing of names and values to a minimum and
allow users to select from values "administrators" want
them to select from. I dont know what the "lookup" table
is about since it replicates the data you already have
stored in the All Audits table. Maybe I am not
understanding the jist of this thing.

With the table structure you sent me, a simple Query will
display both Performance and Finincial Audits in one
query while searching for Audit Dates that are "NULL"

Here is the query:

SELECT tbl_AllAudits.*
FROM tbl_AllAudits
WHERE (((tbl_AllAudits.audittype)="Performance") AND
((tbl_AllAudits.AuditDate) Is Null)) OR
(((tbl_AllAudits.audittype)="Financial") AND
((tbl_AllAudits.AuditDate) Is Null));

You can add a ORDER BY "fieldname" "DESC" (or blank for
ascend) to have the records sorted as you wished. You
may want them grouped by Office or by Audit type or by
both. Here is an example:

ORDER BY tbl_AllAudits.audittype, tbl_AllAudits.office;

This would first sort them by type and them sort them by
office.

You could also use a UNION Query, which is a joining of
two separate queries that give the same fields. This is
a bit more complicated in writing, but accomplishes the
very same thing. It breaks up the WHERE clause in the
above query and places them into their own SELECT
statements. This type of query does not really have to
be used for this case, but I thought I would include it
because there may be a need for something like this in
your future.

SELECT tbl_AllAudits.*
FROM tbl_AllAudits
WHERE (((tbl_AllAudits.audittype)="Performance") AND
((tbl_AllAudits.AuditDate) Is Null))
UNION SELECT tbl_AllAudits.*
FROM tbl_AllAudits
WHERE (((tbl_AllAudits.audittype)="Financial") AND
((tbl_AllAudits.AuditDate) Is Null));


I hope this helped. You could make a report based on
this query. Use the wizard and have the report Group
based on the different fields to allow you an easy view.
Probably group on type and on Office.

AS I read back through your original question, I am not
sure that your questions correspond to one another. If
you still need help on making sure there are not
duplicate entries, let me know. I still think you need
to restructure your tables and that would give you
assurity of no duplicates.

Drew


-----Original Message-----
Thanks for your help again Drew.

OK, this is how it goes:

I have a table which holds data on audits which have been carried out on
different offices. (I do not have any unique / primary keys keys set as
yet.)

There are 2 'types' of audit, 'Financial' and 'Performance' - the former
will have an associated 'contract' number (it is the contract that is
audited), the latter is recorded by the 'dept' whose performance has been
audited (it is the department that is audited).

The table structure is as follows:

tbl_AllAudits (region, office, contract, dept, auditType, risk, fundsAtRisk,
auditDate)

eg for a 'Financial' audit
("North", "Newcastle", "NTN001401", Null,
 
Back
Top