Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of unique values

Tags:

mysql

If I have three columns:

orderNumber, name, email 

and I would like to count how many unique emails are in the table how would I go about doing so?

A statement like:

SELECT count(email) FROM orders 

gives me the total count.

I tried SELECT DISTINCT count(email) FROM orders

but that does not seem to be giving me the numbers I am expecting.

like image 738
thatidiotguy Avatar asked May 22 '13 16:05

thatidiotguy


People also ask

Is there a count distinct function in Excel?

Counting unique / distinct rows in Excel is akin to counting unique and distinct values, with the only difference that you use the COUNTIFS function instead of COUNTIF, which lets you specify several columns to check for unique values.

How do I count unique values in a spreadsheet?

Identify the cell you want to display the count of unique values and enter the formula. The formula to count unique values is =COUNTUNIQUE(A1:A10).


2 Answers

use

SELECT count( DISTINCT(email) ) FROM orders 

Distinct provide unique email ids and then simply count them.

like image 106
Alpesh Gediya Avatar answered Sep 22 '22 21:09

Alpesh Gediya


SELECT  count(DISTINCT(email)) FROM orders 

its different from your posting, since its filters out the duplicates before counting it

like image 38
BvuRVKyUVlViVIc7 Avatar answered Sep 24 '22 21:09

BvuRVKyUVlViVIc7