Am I understanding DISTINCT correctly?

jrg24

New Member
Joined
Aug 6, 2010
Messages
49
I am creating a database to help with some of my financial and accounting responsibilities. I am most certainly a novice at access so any help you can provide will be appreciated.

This database will be used once a month and the source transnational data will be replaced each time.

In the database I will be keeping a customer table which will not be replaced, but I will need to append it based off of the new transaction data. I will be adding all new unique customerIDs to the customer table along with their associated customer names.

I wrote the code below to come up with the list of unique customer ID's in the transaction table. It does appear to return a list of unique customerIDs but I want to clarify if that is indeed what it is doing.

Code:
 SELECT DISTINCT tbl_PRODetail.[Customer ID], tbl_PRODetail.[Customer Name]FROM tbl_PRODetail;

Is the code actually returning all unique customerIDs and their associated names?
Is it returning all unique combinations of CustomerID and CustomerName?
Is it returning all unique customerIDs and unique customer names?

I know that a customer's name does sometime change, but in reality I am not concerned with these changes because even if the name changes, the entity that name represents stays the same (Think someone changing their name after they get married i.e. the customerID will never be used for an actual different customer). I just want to make sure that I have an list of unique customerIDs only. The customer Name should only be a descriptive field for the CustomerID.

The next step is to use this list and append my customer list. Is the code above even taking me in the right direction to achieving this goal?

Thanks for your help.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
DISTINCT checks only the fields listed in the SQL string and then eliminates the duplicate rows in the results. Therefore, no combination of fields across the returned record will be the same from one record to the next.
DISTINCTROW checks all fields in the table that is being queried, and eliminates duplicates based on the entire record. If your ID field is unique as you say, I doubt you have a need for a DISTINCT clause, unless there is something about the data structure and joins between two or more tables that would result in duplicate records. If that's the case, check if you can add a join to make the results more restricted. To answer your question definitively, one would have to know more about your data structure.

With respect to your comment on name changes, it should not be an issue to change a name if the join between this record is based on a primary key (such as ID) and its related foreign key in other tables.
 
Last edited:
Upvote 0
DISTINCT checks only the fields listed in the SQL string and then eliminates the duplicate rows in the results. Therefore, no combination of fields across the returned record will be the same from one record to the next.
DISTINCTROW checks all fields in the table that is being queried, and eliminates duplicates based on the entire record. If your ID field is unique as you say, I doubt you have a need for a DISTINCT clause, unless there is something about the data structure and joins between two or more tables that would result in duplicate records. If that's the case, check if you can add a join to make the results more restricted. To answer your question definitively, one would have to know more about your data structure.

With respect to your comment on name changes, it should not be an issue to change a name if the join between this record is based on a primary key (such as ID) and its related foreign key in other tables.

Thanks for the reply. I believe I have found my answer from a similar question on another forum. Code is below:

Code:
SELECT tbl_PRODetail.[Customer ID], tbl_PRODetail.[Customer Name]FROM tbl_PRODetail LEFT JOIN tbl_Customer ON tbl_PRODetail.[Customer ID] = tbl_Customer.[CustomerID]
WHERE (((tbl_Customer.CustomerID) Is Null));

This code was created by the Query wizard in Access. I will convert this new query into an append query and I should have what I need. Thanks!
 
Upvote 0
Welcome - not that I think I did much to help...
 
Upvote 0
Hi, it looks like you shouldn't use customer name here. For instance, if your query returns:
14548 ABC Incorporated

And you now have a new record:
14548 ABC Inc.

The customer ID is all that really matters. The name is just candy on top. As a matter of housekeeping you might want to update names to their latest versions. In most databases, you will want to have a customer table that has the "one true version of the truth" - one customer ID, and one customer name to go with it.

In fact, if your customer list has a unique index on CustomerID, you can just append all your "new" records and just let the existing CustomerID's silently be discarded.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,572
Members
448,972
Latest member
Shantanu2024

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