SQL Problem

rob_andy

New Member
Joined
Mar 12, 2003
Messages
33
SELECT rCustomer.*, rMachine.machCode
FROM rCustomer INNER JOIN rMachine ON rCustomer.HospitalID = rMachine.HospitalID
WHERE ((Not (rMachine.machCode) Is Null));

I've got 2 tables customer and machine, a customer CAN (doesn't have to) have many machines. So I want to single out the customers with machines using the above query. The problem is it gives a record for each machine so you get duplication of customers i.e.

Cust1 MachineA etc...
Cust1 MachineB etc...
Cust2 MachineA etc...
Cust3 MachineC etc...

The problem is I'm using the query to print labels out so I only need one label per customer. How do I refine the query to only show me one instance of the customer if they have a machine.

Thanks for any help in advance. R
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You need to add the DISTINCT key word to your query.
You can do this in the SQL like
SELECT DISTINCT rCustomer.*, rMachine.machCode
FROM rCustomer INNER JOIN rMachine ON rCustomer.HospitalID = rMachine.HospitalID
WHERE ((Not (rMachine.machCode) Is Null));

or in the design grid select the property sheet for the query and set "Unique Values" to Yes.
You will probably need to reduce the visible fields down to just the ones that you want to use for the lables though.

Peter
 
Upvote 0
Thanks for that, it half works, unfortunately because the machines are not unique I still get some recplication. Any further ideas. Thanks R
 
Upvote 0

Forum statistics

Threads
1,221,558
Messages
6,160,484
Members
451,651
Latest member
Penapensil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top