Q
Problem solve Get help with specific problems with your technologies, process and projects.

Query to show duplicate customers only once

I would like to know how I can write a query that would only show my customers once, even though they are in the database more than that.

Hi my friend. You just have to add the clause DISTINCT:

 SELECT DISTINCT CustomerName FROM Customers
But if you use several data sources (tables, views) then you must use the UNION operator, which automatically combines the result of two or more SELECT statements in one result set and eliminates duplicates records by default.
 SELECT CustomerName FROM Customers UNION SELECT CustomerName2 FROM Customers2
If you need all records (including duplicates), add the option ALL to the UNION operator.
 Select Statement UNION [ALL] Select Statement

 

For More Information

This was last published in February 2002

Dig Deeper on Microsoft SQL Server Installation

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchBusinessAnalytics

SearchDataCenter

SearchDataManagement

SearchAWS

SearchOracle

SearchContentManagement

SearchWindowsServer

Close