473,657 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query returns too many records

6 New Member
Hi
I'm struggling with a query returning too many records.

I have 3 related tables.
tblPatients
tblOperations
tblTargets

Each patient in tblPatients can have more than one operation (one-many)
Each Operation in tblOperations can have many targets (many-many)

The unique key across tables is PtFileNumber

So one patients data would look something like this (only bare bones given)

Expand|Select|Wrap|Line Numbers
  1. (tblPatient)
  2. KENROT56 (ie had 3 operations )
  3.  
  4. (tblOperations)
  5. 12/01/06 (3 records, one operation on each on these dates )
  6. 10/02/06 
  7. 15/05/06
  8.  
  9. (tblTarget)
  10. 12/01/06 (4 records, ie for one operation there are 2  targets)
  11. 12/01/06
  12. 10/02/06
  13. 10/02/06
Data is entered through forms. The patient ID data is entered seperately to Operation data. Target data is entered seperately to operation data - all are linked via the unique field ptFileNumber.

Not all the data is entered at once but it is always entered in order ie Patient ID followed by Operation followed by Target.

I have one query that finds all Patients that do not (yet) have a corresponding Operation - works fine.

Then I have another query that is supposed to return all patients from the operations table that do not already have a matching date in the target table. Here I get the records I want plus records I don't want for SOME patients. So from my lists above I want to see returned ONLY kenrot56 15/05/06 instead I might get kenrot56 15/05/06 AND 10/02/06 .why?

Expand|Select|Wrap|Line Numbers
  1. <<SELECT DISTINCTROW tblOperation.date, tblOperation.PtFileNumber
  2. FROM tblOperation LEFT JOIN tblTargetSites ON tblOperation.PtFileNumber = tblTargetSites.ptFileNumber
  3. WHERE (((tblOperation.date) In (SELECT [date] FROM [tblOperation] where [date] <> [operationdate])))
  4. ORDER BY tblOperation.date;>>

sorry to be long winded Does this make sense?

Cheers
Gem
Feb 11 '07 #1
8 9078
MMcCarthy
14,534 Recognized Expert Moderator MVP
Hi
I'm struggling with a query returning too many records.

I have 3 related tables.
tblPatients
tblOperations
tblTargets

Each patient in tblPatients can have more than one operation (one-many)
Each Operation in tblOperations can have many targets (many-many)

The unique key across tables is PtFileNumber
Gem

Before we even look at the query we need to deal with your table structure. Tables cannot have a many to many relationship. This can be resolved by the use of a JOIN table.

tblOperations
OperationID (Primary Key - Unique)

tblTargets
TargetID (Primary Key - Unique)

New table:

tblTargetPerOpe ration

OperationID (Foreign Key referencing tblOperations Primary key)
TargetID (Foreign Key referencing tblTargets Primary key)

Both of these represent the Primary Key of this new table. Neither will be unique in itself but together the combination will be unique.

Check out this tutorial for more information.

Normalisation and Table structures

Mary
Feb 11 '07 #2
NeoPa
32,568 Recognized Expert Moderator MVP
Are tblOperation and tblTargetSites (I'm using your SQL in preference to your explanation as they contradict each other) really many-to-many or are they one-to-many but underneath the tblPatient table? Is it really possible to have a single tblTargetSites record shared between two separate tblOperation records?

The answer to this is fundamental in knowing what the next step should be.
Feb 11 '07 #3
Gem
6 New Member
OK, yes I see, you are both saying the same thing - the tblOperation and tbleTarget is in fact one-to-many (one operation can have many targets). In tblOperation and tblTargesite I had put the default record number as the primary key and in tblpatient the pk is pkPatientFile number. I see where I went wrong. I will put in the join table.

thanks a million
Gem
Feb 11 '07 #4
Gem
6 New Member
Hi Guys
It's late and I'm going to ask a really dumb question - Now that I have my Join table how do I populate the thing with the exisitng records? I have 164 records in tblOperation and 463 in tblTargetsites

I tried using an append query based on tblOperation and tblTargetsites but just ended up duplicating the original problem ie. too many records - so that was not good.

Also, how do I ensure that all rew records also appear in this join table. Most of my forms are based on queries do I add this new table to the underlying queries - doesn't feel quite right.

cheers
Gem
Feb 12 '07 #5
NeoPa
32,568 Recognized Expert Moderator MVP
If it's a one-to-many relationship you don't need a Join Table after all. That's why I asked.
All you need is a PK (one) and a matching FK (many).

Mary's answer was based on what you'd posted earlier.
Feb 12 '07 #6
Gem
6 New Member
If it's a one-to-many relationship you don't need a Join Table after all. That's why I asked.
All you need is a PK (one) and a matching FK (many).

Mary's answer was based on what you'd posted earlier.

Hi NeoPa
I'm guilty of not paying attention..... thank you for replying.

Here's what I have now

Primary Table
tblPatient
Record autoNo.
pkPtFileNumber (indexed No duplicates)

Second Table
tblOperation
pkOperationID autoNo. (indexed No duplicates)
fkPtFileNumer (indexed Duplicates OK)

Third Table
tblTargetsites
pkTargetSiteID (indexed No Duplicates)
fkOperationID (indexed Duplicates OK)

in the database relationships window I have enforced referential integrity with cascade update and delete. The join type btwn tblPatient & tblOperation is 2 (because I want to see a complete list of patients whether or not they have operations)
the join btwn tblOperation and tblTargetsites is also 2.

Now, the fkPtFileNumber is fully populated (because I use it in a combobox on frmOperation to ID the patient ) BUT the fkOperationID in tblTargetSites is empty and I have to work out how to match the fk with the 400 records that are already in tblTargetSites.

Gem
Feb 13 '07 #7
MMcCarthy
14,534 Recognized Expert Moderator MVP
Hi NeoPa
I'm guilty of not paying attention..... thank you for replying.

Here's what I have now

Primary Table
tblPatient
Record autoNo.
pkPtFileNumber (indexed No duplicates)

Second Table
tblOperation
pkOperationID autoNo. (indexed No duplicates)
fkPtFileNumer (indexed Duplicates OK)

Third Table
tblTargetsites
pkTargetSiteID (indexed No Duplicates)
fkOperationID (indexed Duplicates OK)

in the database relationships window I have enforced referential integrity with cascade update and delete. The join type btwn tblPatient & tblOperation is 2 (because I want to see a complete list of patients whether or not they have operations)
the join btwn tblOperation and tblTargetsites is also 2.

Now, the fkPtFileNumber is fully populated (because I use it in a combobox on frmOperation to ID the patient ) BUT the fkOperationID in tblTargetSites is empty and I have to work out how to match the fk with the 400 records that are already in tblTargetSites.

Gem
Do you have this data stored somewhere else Gem like a spreadsheet or something.

Mary
Feb 13 '07 #8
NeoPa
32,568 Recognized Expert Moderator MVP
Most posters fail even to realise why attention to detail is even required, so a single slip is certainly no great problem.
With reference to your earlier question, this makes the structure hierarchical and so a lot simpler. The wizards should be able to handle your data quite normally and at least give you a basic structure to build on.
I'm not sure how to make the links between your tables if you don't have any existing data to link them with I'm afraid.
Feb 13 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
3823
by: Tanamon | last post by:
Hello All, I am a MySQL newbie trying to write a query that selects file_name records possessing the highest numbered version for that unique file_name. I show sample data and two trial queries below. Logically I want to use max(version) as a constraint in a Where Clause. However, the max() function is not allowed directly in a where clause. I have contemplated a second table to track the max version for each file name. I would like...
3
4979
by: Steve | last post by:
Form FrmRestock's recordsource is QryFrmRestock. The TransactionDate field's criteria is set ats: Forms!FrmRestock!LastXDays. LastXDays on the form is a combobox where the selections are 30, 60 and 90. The default is set at 30. Question1: When the form opens, there are no records displayed although there are many records that fit the criteria of 30. If I put a button on the form to do a requery and press the button, all the records...
3
4600
by: Tom Mitchell | last post by:
All: I'm stumped on a query. How do I find duplicates in a table where one of the duplicates has values is a certain field and the other doesn't. For example, I have the following table: tblItems ItemID ItemLabel
2
1781
by: Justin Koivisto | last post by:
In PHP, I do the following (pseudo code for clarity)... Query: SELECT distinct(web_contacts.zip) FROM web_contacts WHERE web_contacts.zip <> '' Query: SELECT DISTINCT(contacts.zip) FROM contacts WHERE contacts.zip <> ''; Take all those results put them into an array: $zips = 0; The zipCode is used as the index to avoid duplicates between tables...
14
8075
by: Darin | last post by:
I have a table that I want to delete specific records from based on data in other tables. I'm more familiar with Access '97, but am now using 2003, but the database is in 2000 format. In '97, I think I could have easily done this using joins, but I kept getting "could not delete from specified tables" errors. Some google searching has indicated I need to use a subquery. After many failed attempts with different approaches, I finally...
10
4576
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a zero-length string (always returns a zero-length string when used in a query expression)" **** How many records are there in FirstTable in which Product Is Null. SELECT COUNT(*) AS CountofNullProdcut
2
2385
by: Fendi Baba | last post by:
I created a person table with various fields such as Suffix, Salutation, etc, Some of these fields may not be mandatory for example suffix. In the actual table itself, I only have a field for suffix ID where 1=Phd, 2= MD. To display all of these to the user, I created a form with an underlying query. The problem I am encountering is this, when we have an empty field, for example where ID="", the query returns nothing. How do i work around...
5
3519
by: Wired Hosting News | last post by:
I tried to be breif and give a scenario so as not to be overlooked because it was soooo long. Let me give you real world. I am a manufacturer of goods and produce 11 items that are distributed to 1800 stores of a national home improvement chain store. Every week I electronicaly receive an excel spreadsheet "inventory report" with 19,800 rows or records, which I import into my tblSalesData table. The table now has 10 weeks of data or...
9
2981
by: Bob Darlington | last post by:
The following query opens slowly the first time it is opened (6-7 seconds), but then is less than one second for the next random number of openings before slowing (6-7 seconds) again. SELECT tTenantDetails.LAN, tTenants.Archive, tTenants.PropNum FROM tTenants INNER JOIN tTenantDetails ON tTenants.LAN = tTenantDetails.LAN WHERE (((tTenants.PropNum)=10)); I'm using Access 2002 with XP Pro OS and 2GB RAM. tTenants and tTenantDetails have...
0
8395
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
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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
8503
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,...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.