Solved

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

Posted on 2014-10-21
4
410 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 65

Accepted Solution

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql views 3 55
connection to SQL 2012 error in windows 10 18 44
Report 8 26
How to build a logic for passwords according to initials? 13 43
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

739 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