Solved

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

Posted on 2014-10-21
4
393 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
  • 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBScript Write Column Headers 3 38
Sql Server group by 10 29
Tsql query 6 22
what is the easiest way and cheapest way to build a simple database that can be accessed online 10 42
Creating and Managing Databases with phpMyAdmin in cPanel.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

810 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