Solved

Total the number of unique customer numbers in a table with an sql statement

Posted on 2014-10-21
4
418 Views
Last Modified: 2014-10-21
I have a table with a customer number field. The customer number can be in the table multiple times.  I need to total of the number of unique customer numbers in the file. In other words, how many customers do I have?

How can I do this in sql?
0
Comment
Question by:roosterup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40394469
SELECT COUNT(DISTINCT CustomerNumber)
FROM YourTable
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40394509
Thanks for the grade.  I have an article out there on SQL Server GROUP BY Solutions, and just noticed that COUNT(DISTINCT) is not on it, so I'll have to add it.  Thanks.
0
 
LVL 9

Expert Comment

by:Lieven Embrechts
ID: 40394510
The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column:
eg. : SELECT COUNT(DISTINCT column_name) FROM table_name;
Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with Microsoft Access.

Example:
The following SQL statement counts the number of unique customers in the "Orders" table:
SELECT COUNT(DISTINCT CustomerID) AS NumberOfCustomers FROM Orders;

When count(distinct) is not supported (as in access), you can split the count and distinct in a sub-query:
Example:
SELECT count(*)
FROM
(SELECT DISTINCT CustNbr FROM CustTable1)
0
 
LVL 9

Expert Comment

by:Lieven Embrechts
ID: 40394633
ok, thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question